python 中使用 gevent 的静态 Web 服务器演示
a static web server demo used gevent in python
我的静态 Web 服务器演示存在一些问题 python.when 我的浏览器使用 gevent 访问我的 Web 服务器 http://localhost:8080.It 没有任何 response.If 我的静态 Web 服务器演示别用gevent,效果很好,肯定是gevent有问题
#coding=utf-8
from socket import *
from gevent import monkey
import re
import gevent
monkey.patch_all()
def handle_client(client_socket):
recv_data = client_socket.recv(1024).decode("utf-8")
response_header_lines = recv_data.splitlines()
for line in response_header_lines:
print(line)
http_request_line = response_header_lines[0]
get_file_name = re.match("[^/]+(/[^ ]*)", http_request_line).group(1)
print("file name is ===>%s"%get_file_name) #for test
if get_file_name == "/":
get_file_name = DOCUMENTS_ROOT + "/index.html"
else:
get_file_name = DOCUMENTS_ROOT + get_file_name
print("file name is ===2>%s"%get_file_name)
try:
f = open(get_file_name, "rb")
except IOError:
response_header = "HTTP/1.1 404 not found\r\n"
response_header += "\r\n"
response_body = "404 not found"
else:
response_header = "HTTP/1.1 200 OK\r\n"
response_header += "\r\n"
response_body = f.read()
f.close()
finally:
client_socket.send(response_header.encode("utf-8"))
client_socket.send(response_body)
client_socket.close()
def main():
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server_socket.bind(("", 8080))
server_socket.listen(128)
while True:
client_socket, client_addr = server_socket.accept()
#handle_client(client_socket)
gevent.spawn(handle_client, client_socket)
server_socket.close()
DOCUMENTS_ROOT = "./html"
if __name__ == "__main__":
main()
你应该应用猴子补丁 before import any module that would be patched, socket
这里有问题:
from gevent import monkey
monkey.patch_all()
from socket import *
...
its doc强调:
Patching should be done as early as possible in the lifecycle of the
program. For example, the main module (the one that tests against
main or is otherwise the first imported) should begin with this code, ideally before any other imports
我的静态 Web 服务器演示存在一些问题 python.when 我的浏览器使用 gevent 访问我的 Web 服务器 http://localhost:8080.It 没有任何 response.If 我的静态 Web 服务器演示别用gevent,效果很好,肯定是gevent有问题
#coding=utf-8
from socket import *
from gevent import monkey
import re
import gevent
monkey.patch_all()
def handle_client(client_socket):
recv_data = client_socket.recv(1024).decode("utf-8")
response_header_lines = recv_data.splitlines()
for line in response_header_lines:
print(line)
http_request_line = response_header_lines[0]
get_file_name = re.match("[^/]+(/[^ ]*)", http_request_line).group(1)
print("file name is ===>%s"%get_file_name) #for test
if get_file_name == "/":
get_file_name = DOCUMENTS_ROOT + "/index.html"
else:
get_file_name = DOCUMENTS_ROOT + get_file_name
print("file name is ===2>%s"%get_file_name)
try:
f = open(get_file_name, "rb")
except IOError:
response_header = "HTTP/1.1 404 not found\r\n"
response_header += "\r\n"
response_body = "404 not found"
else:
response_header = "HTTP/1.1 200 OK\r\n"
response_header += "\r\n"
response_body = f.read()
f.close()
finally:
client_socket.send(response_header.encode("utf-8"))
client_socket.send(response_body)
client_socket.close()
def main():
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server_socket.bind(("", 8080))
server_socket.listen(128)
while True:
client_socket, client_addr = server_socket.accept()
#handle_client(client_socket)
gevent.spawn(handle_client, client_socket)
server_socket.close()
DOCUMENTS_ROOT = "./html"
if __name__ == "__main__":
main()
你应该应用猴子补丁 before import any module that would be patched, socket
这里有问题:
from gevent import monkey
monkey.patch_all()
from socket import *
...
its doc强调:
Patching should be done as early as possible in the lifecycle of the program. For example, the main module (the one that tests against main or is otherwise the first imported) should begin with this code, ideally before any other imports