Simple HTTP Server and Client in Python
来源:互联网 发布:ant java 版本 编辑:程序博客网 时间:2024/04/29 16:30
This time I'd like to show you how to make a simple HTTP server and client in python. It's a bit different from other tutorials I've ever wrote and I'd like to say that I'm also a beginner in python. But, I'll try to make sure you understand what I wrote because this tutorial is easy.
First, you need to know what HTTP is. Definition of HTTP based on wikipedia:
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web.
In simple words, HTTP is a protocol that used for world wide web communication. So, if you're browsing the web, downloading data, and hosting a website, you're using HTTP protocol. There are many web server software that support HTTP protocol such as apache, nginx, and lighttpd. This time, you're so lucky because I'll show you how to make something like that (a simple version of course) in python language.
Step #1: Write HTTP server script using BaseHTTPServer module
Luckily, python provide us an HTTP server module, it's called
BaseHTTPServer
. We use two classes from this module, namely BaseHTTPRequestHandler
and HTTPServer
. We also need to use os module to access file in your system.First, we need to import those modules
#!/usr/bin/env pythonfrom BaseHTTPServer import BaseHTTPRequestHandler, HTTPServerimport os
Next, write a custom class namely
KodeFunHTTPRequestHandler
that implementBaseHTTPRequestHandler
. #Create custom HTTPRequestHandler classclass KodeFunHTTPRequestHandler(BaseHTTPRequestHandler):
In this tutorial, I just implement GET command from HTTP. To do that, we need to override
do_GET()
function. Our objective is to request an HTML file from server using our client (explained int the next step). Script insidedo_GET()
function is written to handle our objective.#handle GET command def do_GET(self): rootdir = 'c:/xampp/htdocs/' #file location try: if self.path.endswith('.html'): f = open(rootdir + self.path) #open requested file #send code 200 response self.send_response(200) #send header first self.send_header('Content-type','text-html') self.end_headers() #send file content to client self.wfile.write(f.read()) f.close() return except IOError: self.send_error(404, 'file not found')
Finally, add following script to run the server
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.serve_forever() if __name__ == '__main__': run()
Save it and name it whatever you want (e.g, kodefun-httpserver.py). Here is the complete script of http server:
#!/usr/bin/env pythonfrom BaseHTTPServer import BaseHTTPRequestHandler, HTTPServerimport os#Create custom HTTPRequestHandler classclass KodeFunHTTPRequestHandler(BaseHTTPRequestHandler): #handle GET command def do_GET(self): rootdir = 'c:/xampp/htdocs/' #file location try: if self.path.endswith('.html'): f = open(rootdir + self.path) #open requested file #send code 200 response self.send_response(200) #send header first self.send_header('Content-type','text-html') self.end_headers() #send file content to client self.wfile.write(f.read()) f.close() return except IOError: self.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.serve_forever() if __name__ == '__main__': run()
Step #2: Write a simple HTTP client
To check if our server is working fine, we need an HTTP client. You can use your web browser to do this. But, It'll be cool if you make your own client. To accomplish this, let's make another script, let's name it kodefun-httpclient.py. Copy following script to your file:
#!/usr/bin/env pythonimport httplibimport sys#get http server iphttp_server = sys.argv[1]#create a connectionconn = httplib.HTTPConnection(http_server)while 1: cmd = raw_input('input command (ex. GET index.html): ') cmd = cmd.split() if cmd[0] == 'exit': #tipe exit to end it break #request command to server conn.request(cmd[0], cmd[1]) #get response from server rsp = conn.getresponse() #print server response and data print(rsp.status, rsp.reason) data_received = rsp.read() print(data_received) conn.close()
Step #3: Test using GET command
The final step, we'll test is our scripts above work just fine. To run the server, type the following command in your command prompt or terminal:
python kodefun-httpserver.py
If there is no problem, you'll see "http server is running...". Next, we need to create a dummy HTML file, let's call it dummy.html and copy the code below:
<head> <title>Dummy HTML</title></head><body> <h1>Kode Fun is Awesome</h1> <h1>You're awesome</h1></body>
Next, we need to run the client, type following command to run the client:
python kodefun-httpclient.py 127.0.0.1
When the client is running type following code to get dummy.html file:
GET dummy.html
If there is no problem, and it should be no problem, you'll see the content of dummy.html file.
That's all I can do this time. My upcoming tutorial is how to perform face detection using Accord.NET. Stay tune and keep fighting!!!
0 0
- Simple HTTP Server and Client in Python
- Write a simple HTTP server in Python
- Simple UDP server and client
- simple HTTP server - python
- python simple http server
- python client and server
- python server and client
- Simple OAuth Client and Server Examples
- Simple TCP Socket Client and Server Communication in C Under Linux
- python https server and client
- Python UDP Server and Client
- java nio mina 学习笔记1 Simple Server and Client
- Simple client-server network using C++ and Windows Winsock
- HTTP Operational Model and Client/Server Communication
- dhcp server and client in ubuntu12.04
- Really Simple HTTP Server with Python
- Making a simple web server in Python
- python创建http/https server和client
- linux内核 asmlinkage宏
- linux 命令之 rm,rmdir 详解
- POJ3181 Dollar Dayz
- 每天一个C++小程序(十九)--桶排序
- 不要一辈子靠技术生存
- Simple HTTP Server and Client in Python
- python在windows下安装第三方库
- VC++小技巧-Output窗口输出定位 .
- POJ 2886 Who Gets the Most Candies(线段树+约瑟夫环)
- PHP中的字符串 — 表示方法
- C语言 八皇后问题
- 转载中医偏方
- 阿里云搭建FTP服务器(非常适合新手)
- Python<7>while、for循环