本文主要是介绍threading、multiprocessing、gevent的简单使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、threading
import re
import socket
import threading# 定义服务函数为客户端服务
def server(new_socket):# 接收请求并解码request = new_socket.recv(1024).decode("utf-8")# 将请求分割成行request_lines = request.splitlines()# 使用正则匹配请求的第一行,内容大概为:GET / HTTP/1.1result = re.match(r'[^/]+(/[^ ]*)', request_lines[0])# 如果有匹配到内容,查找并读取对应文件内容if result:file_name = result.group(1)if file_name == "/":file_name = "/login.html"try:f = open(r'D:\testFile\web' + file_name, 'rb')# 否则发送404页面except:response = "HTTP/1.1 404 NOT FOUND\r\n"response += '\r\n'response += "--------404-----------"new_socket.send(response.encode("utf-8"))# 发送header和bodyelse:response_body = f.read()f.close()response_header = "HTTP/1.1 200 OK\r\n"response_header += '\r\n'# 发送headernew_socket.send(response_header.encode("utf-8"))# 发送bodynew_socket.send(response_body)# 关闭服务套接字new_socket.close()def main():# 创建套接字tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 绑定ip和port,双引号内空格为空表示绑定本地iptcp_server.bind(("", 58080))# 监听tcp_server.listen(128)# 等待客户端请求while True:# 创建服务套接字,为请求客户端服务new_socket, client_addr = tcp_server.accept()# 调用服务函数t = threading.Thread(target=server, args=(new_socket, ))t.start()# 关闭套接字tcp_server.close()if __name__ == '__main__':main()
2、multiprocessing
import re
import socket
import multiprocessingdef server(new_socket):request = new_socket.recv(1024).decode("utf-8")request_lines = request.splitlines()result = re.match(r'[^/]+(/[^ ]*)', request_lines[0])# 为客户端服务if result:file_name = result.group(1)if file_name == "/":file_name = "/login.html"try:f = open(r'D:\testFile\web' + file_name, 'rb')except:response = "HTTP/1.1 404 NOT FOUND\r\n"response += '\r\n'response += "--------404-----------"new_socket.send(response.encode("utf-8"))else:response_body = f.read()f.close()response_header = "HTTP/1.1 200 OK\r\n"response_header += '\r\n'# 发送headernew_socket.send(response_header.encode("utf-8"))# 发送bodynew_socket.send(response_body)new_socket.close()def main():# 创建套接字tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 绑定tcp_server.bind(("", 58080))# 监听tcp_server.listen(128)# 等待客户端请求while True:new_socket, client_addr = tcp_server.accept()p = multiprocessing.Process(target=server, args=(new_socket,))p.start()# 创建子进程时,会复制父进程全局变量和局部变量:new_socket,子进程和父进程都需要关闭服务套接字,才最终停止发送数据new_socket.close()tcp_server.close()if __name__ == '__main__':main()
3、gevent
import re
import socket
import gevent
from gevent import monkeymonkey.patch_all()def server(new_socket):request = new_socket.recv(1024).decode("utf-8")print(request)request_lines = request.splitlines()result = re.match(r'[^/]+(/[^ ]*)', request_lines[0])# 为客户端服务if result:file_name = result.group(1)if file_name == "/":file_name = "/login.html"try:f = open(r'D:\testFile\web' + file_name, 'rb')except:response = "HTTP/1.1 404 NOT FOUND\r\n"response += '\r\n'response += "--------404-----------"new_socket.send(response.encode("utf-8"))else:response_body = f.read()f.close()response_header = "HTTP/1.1 200 OK\r\n"response_header += '\r\n'# 发送headernew_socket.send(response_header.encode("utf-8"))# 发送bodynew_socket.send(response_body)new_socket.close()def main():# 创建套接字tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 绑定tcp_socket.bind(("", 58080))# 监听tcp_socket.listen(128)# 等待客户端请求while True:new_socket, client_addr = tcp_socket.accept()gevent.spawn(server, new_socket)tcp_socket.close()if __name__ == '__main__':main()
这篇关于threading、multiprocessing、gevent的简单使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!