Python网络应用也需要网络协议的相关知识,可参考协议森林
**注意,在Python 3.x中,BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer整合到http.server包,SocketServer改名为socketserver,请注意查阅官方文档。
在上一篇文章中(用socket写一个Python服务器),我使用socket接口,制作了一个处理HTTP请求的Python服务器。任何一台装有操作系统和Python解释器的计算机,都可以作为HTTP服务器使用。我将在这里不断改写上一篇文章中的程序,引入更高级的Python包,以写出更成熟的Python服务器。
我首先增加该服务器的功能。这里增添了表格,以及处理表格提交数据的”POST”方法。如果你已经读过用socket写一个Python服务器,会发现这里只是增加很少的一点内容。
原始程序:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 | # Written by Vamei# A messy HTTP server based on TCP socket import socket # AddressHOST = \’\’PORT = 8000 text_content = \’\’\’HTTP/1.x 200 OK Content-Type: text/html Wow, Python Server ![]() \’\’\’ f = open(\’test.jpg\’,\’rb\’)pic_content = \’\’\’HTTP/1.x 200 OK Content-Type: image/jpg \’\’\’pic_content = pic_content + f.read() # Configure sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind((HOST, PORT)) # Serve foreverwhile True: s.listen(3) conn, addr = s.accept() request = conn.recv(1024) # 1024 is the receiving buffer size method = request.split(\’ \’)[0] src = request.split(\’ \’)[1] print \’Connected by\’, addr print \’Request is:\’, request # if GET method request if method == \’GET\’: # if ULR is /test.jpg if src == \’/test.jpg\’: content = pic_content else: content = text_content # send message conn.sendall(content) # if POST method request if method == \’POST\’: form = request.split(\’\\r\\n\’) idx = form.index(\’\’) # Find the empty line entry = form[idx:] # Main content of the request value = entry[–1].split(\’=\’)[–1] conn.sendall(text_content + \’\\n \’ + value + \'\’) ###### # More operations, such as put the form into database # … ###### # close connection conn.close() |
服务器进行的操作很简单,即从POST请求中提取数据,再显示在屏幕上。
运行上面Python服务器,像上一篇文章那样,使用一个浏览器打开。
页面新增了表格和提交(submit)按钮。在表格中输入aa并提交,页面显示出aa。
我下一步要用一些高级包,来简化之前的代码。
首先使用SocketServer包来方便的架设服务器。在上面使用socket的过程中,我们先设置了socket的类型,然后依次调用bind(),listen(),accept(),最后使用while循环来让服务器不断的接受请求。上面的这些步骤可以通过SocketServer包来简化。
SocketServer:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 | # Written by Vamei# A messy HTTP server based on TCP socket import socket # AddressHOST = \’\’PORT = 8000 text_content = \’\’\’HTTP/1.x 200 OK Content-Type: text/html Wow, Python Server ![]() \’\’\’ f = open(\’test.jpg\’,\’rb\’)pic_content = \’\’\’HTTP/1.x 200 OK Content-Type: image/jpg \’\’\’pic_content = pic_content + f.read() # Configure sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind((HOST, PORT)) # Serve foreverwhile True: s.listen(3) conn, addr = s.accept() request = conn.recv(1024) # 1024 is the receiving buffer size method = request.split(\’ \’)[0] src = request.split(\’ \’)[1] print \’Connected by\’, addr print \’Request is:\’, request # if GET method request if method == \’GET\’: # if ULR is /test.jpg if src == \’/test.jpg\’: content = pic_content else: content = text_content # send message conn.sendall(content) # if POST method request if method == \’POST\’: form = request.split(\’\\r\\n\’) idx = form.index(\’\’) # Find the empty line entry = form[idx:] # Main content of the request value = entry[–1].split(\’=\’)[–1] conn.sendall(text_content + \’\\n \’ + value + \'\’) ###### # More operations, such as put the form into database # … ###### # close connection conn.close() |
服务器进行的操作很简单,即从POST请求中提取数据,再显示在屏幕上。
运行上面Python服务器,像上一篇文章那样,使用一个浏览器打开。
页面新增了表格和提交(submit)按钮。在表格中输入aa并提交,页面显示出aa。
我下一步要用一些高级包,来简化之前的代码。
首先使用SocketServer包来方便的架设服务器。在上面使用socket的过程中,我们先设置了socket的类型,然后依次调用bind(),listen(),accept(),最后使用while循环来让服务器不断的接受请求。上面的这些步骤可以通过SocketServer包来简化。
SocketServer: