Python 快速教程(网络02):Python服务器进化
admin
2023-07-31 00:36:12
0

Python网络应用也需要网络协议的相关知识,可参考协议森林

**注意,在Python 3.x中,BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer整合到http.server包,SocketServer改名为socketserver,请注意查阅官方文档。

在上一篇文章中(用socket写一个Python服务器),我使用socket接口,制作了一个处理HTTP请求的Python服务器。任何一台装有操作系统和Python解释器的计算机,都可以作为HTTP服务器使用。我将在这里不断改写上一篇文章中的程序,引入更高级的Python包,以写出更成熟的Python服务器。

 

支持POST

我首先增加该服务器的功能。这里增添了表格,以及处理表格提交数据的”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

Wow, Python Server

First name:
\’\’\’ 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

首先使用SocketServer包来方便的架设服务器。在上面使用socket的过程中,我们先设置了socket的类型,然后依次调用bind(),listen(),accept(),最后使用while循环来让服务器不断的接受请求。上面的这些步骤可以通过SocketServer包来简化。

SocketServer:

 

value + \'

\’)
######
# More operations, such as put the form into database
# …
######

# Create the server
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
# Start the server, and work forever
server.serve_forever()

方文档。

在上一篇文章中(用socket写一个Python服务器),我使用socket接口,制作了一个处理HTTP请求的Python服务器。任何一台装有操作系统和Python解释器的计算机,都可以作为HTTP服务器使用。我将在这里不断改写上一篇文章中的程序,引入更高级的Python包,以写出更成熟的Python服务器。

 

支持POST

我首先增加该服务器的功能。这里增添了表格,以及处理表格提交数据的”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

Wow, Python Server

First name:
\’\’\’ 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

首先使用SocketServer包来方便的架设服务器。在上面使用socket的过程中,我们先设置了socket的类型,然后依次调用bind(),listen(),accept(),最后使用while循环来让服务器不断的接受请求。上面的这些步骤可以通过SocketServer包来简化。

SocketServer:

 

d class=\”crayon-nums \” data-settings=\”show\”>
123

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...