首页 > 代码库 > python_day9 多进程socket

python_day9 多进程socket

基于多进程 server 端 同时处理多个socket客户端 

from multiprocessing import Process
from socket import *
import time
server=socket(AF_INET,SOCK_STREAM)
server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
server.bind((‘127.0.0.1‘,80))
server.listen(5)
def sock_server(conn,addr):
while True:
try:
data=http://www.mamicode.com/conn.recv(1024)
if not data:continue
print(data.decode(‘utf-8‘))
conn.send(data.upper())
except Exception as e:
print(e)
break
# conn.close()
# server.close()
if __name__ == ‘__main__‘:
while True:
conn,addr=server.accept()
p1=Process(target=sock_server,args=(conn,addr))
p1.start()

python_day9 多进程socket