![python http client example python http client example](https://images.contentstack.io/v3/assets/bltefdd0b53724fa2ce/blt66d390672b99ed42/5fc5223a42256d5ffdf4798d/blog-auto-python-client-1.gif)
When the client is running type following code to get dummy.html file: GET dummy.html Next, we need to run the client, type following command to run the client: python kodefun-httpclient.py 127.0.0.1 Save it to your root folder defined in your script. Next, we need to create a dummy HTML file, let’s call it dummy.html and copy the code below: Dummy HTML Kode Fun is Awesome You're awesome If there is no problem, you’ll see "http server is running.". To run the server, type the following command in your command prompt or terminal: python kodefun-httpserver.py The final step, we’ll test is our scripts above work just fine. getresponse () #print server response and data print ( rsp. request ( cmd, cmd ) #get response from server rsp = conn. split () if cmd = 'exit' : #tipe exit to end it break #request command to server conn. HTTPConnection ( http_server ) while 1 : cmd = raw_input ( 'input command (ex. argv #create a connection conn = httplib. #!/usr/bin/env python import httplib import sys #get http server ip http_server = sys.
![python http client example python http client example](https://www.codegrepper.com/codeimages/python-socket-create-server.png)
The following code snippet is the implementation of our simple HTTP client. This will loop forever until user decide to type exit as a command. It’ll download an HTML file by typing command GET file.html. This script only have two responsibilites based on user entry. To accomplish this, let’s make another script, let’s name it kodefun-httpclient.py. To check if our server is working fine, we need an HTTP client.īut, It’ll be cool if you make your own client. serve_forever () if _name_ = '_main_' : run () Step 2: Write a simple HTTP client send_error ( 404, 'file not found' ) def run (): print ( 'http server is starting.' ) #ip and port of servr #by default http server port is 80 server_address = ( '127.0.0.1', 80 ) httpd = HTTPServer ( server_address, KodeFunHTTPRequestHandler ) print ( 'http server is running.' ) httpd. end_headers () #send file content to client self. send_header ( 'Content-type', 'text-html' ) self. send_response ( 200 ) #send header first self. path ) #open requested file #send code 200 response self.
![python http client example python http client example](https://r00t4bl3.com/uploads/screenshot-from-2019-04-23-15-49-39-ab8851eeb447e112f2a62d372cb00549.png)
endswith ( '.html' ): f = open ( rootdir + self. #!/usr/bin/env python from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import os #Create custom HTTPRequestHandler class class KodeFunHTTPRequestHandler ( BaseHTTPRequestHandler ): #handle GET command def do_GET ( self ): rootdir = 'c:/xampp/htdocs/' #file location try : if self. The following snipped is the complete script of http server. Save it and name it whatever you want (e.g, kodefun-httpserver.py). serve_forever () if _name_ = '_main_' : run () def run (): print ( 'http server is starting.' ) #ip and port of server #by default http server port is 80 server_address = ( '127.0.0.1', 80 ) httpd = HTTPServer ( server_address, KodeFunHTTPRequestHandler ) print ( 'http server is running.' ) httpd. We also need to pass HTTP request handler above as its parameter.įinally, to make the server always run, we only need to call server_forever() function. We only need to create an HTTPServer object with defined IP address and port. #handle GET command def do_GET ( self ): rootdir = 'c:/xampp/htdocs/' #file location try : if self. Script inside do_GET() function is written to handle our objective. Our objective is to request an HTML file from server using our client (explained int the next step). To do that, we need to override do_GET() function. In this tutorial, I just implement GET command from HTTP.
![python http client example python http client example](https://i1.wp.com/www.bytetips.com/wp-content/uploads/2017/05/ClientServer-Project.png)
#Create custom HTTPRequestHandler class class KodeFunHTTPRequestHandler ( BaseHTTPRequestHandler ): Next, write a custom class namely KodeFunHTTPRequestHandler that implement BaseHTTPRequestHandler. We also need to use os module to access file in your system.įirst, we need to import those modules: #!/usr/bin/env python from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import os We use two classes from this module, namely BaseHTTPRequestHandler and HTTPServer. Luckily, python provides us an HTTP server module, it’s called BaseHTTPServer. Step 1: Write HTTP server script using BaseHTTPServer module