服務端:多程序和多執行緒的開啟方式相同。
缺點:<1> 由於cpython的gil,導致同一時間無法執行多個執行緒;<2> 不可能無限開進程式或執行緒
解決辦法:多程序、concurrent.futures.processpoolexecutor、執行緒池
import socket
from multiprocessing import process
from threading import thread
class mytcpserver:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.server = socket.socket()
self.server.bind((self.ip, self.port))
self.server.listen(5)
def wait_accept(self):
conn, addr = self.server.accept()
return conn, addr
def handle_request(self, conn):
while 1:
try:
data = conn.recv(1024)
if not data: break
conn.send(data.upper())
except exception as e:
print(e)
break
conn.close()
if __name__ == '__main__':
server = mytcpserver('127.0.0.1', 8888)
while 1:
conn, addr = server.wait_accept()
p = process(target=server.handle_request, args=(conn, )) # 建立乙個程序
p.start() # 告訴操作提供,開啟這個程序
非同步提交任務,支援非同步接收返回結果(submit返回乙個futures物件,呼叫add_done_callback方法)
import socket
from concurrent.futures import processpoolexecutor
# from concurrent.futures import threadpoolexecutor
class mytcpserver:
def __init__(sel ip, port):
self.ip = ip
self.port = port
self.server = socket.socket()
self.server.bind((self.ip, self.port))
self.server.listen(5)
def wait_accep程式設計客棧t(self):
conn, addr = self.server.accept()
return conn, addr
def handle_request(self, conwww.cppcns.comn):
while 1:
try:
data = conn.recv(1024)
if not data: break
conn.send(data.upper())
except exception as e:
print(e)
break
conn.close()
if __name__ == '__main__':
server = mytcpserver('127.0.0.1', 8888)
pool = processpoolexecutor(5) # 5個程序一直服務
while 1:
conn, addr = server.wait_accept()
pool.submit(server.handle_request, conn) # 非同步提交任務
優點:簡化socket服務端建立流程。
提供服務端序列和併發兩種服務模式(tcpserver,threadingtcpserver)
缺點:windows上無法使用多程序實現併發
import socketserver
class mytcphandler(socketserver.baserequesthandler):
def handle(self): # 通訊迴圈
while 1:
try:
data = self.request.recv(1024)
if not data: break
self.request.send(data.upper())
except exception as e:
print(e)
break
self.request.close()
if __name__ == '__main__':
ip_port = '127.0.0.1', 8888
server = socketserver.threadingtcpserver(ip_port, mytcphandler) # 非同步處理
server.serve_forever() # 連線迴圈
協程優點:單執行緒內實現併發,**級別模擬io切換,提高程式執行效率
from gevent import spawn, monkey;monkey.patch_all() # 猴子補丁,補丁:常規io
import socket
class mytcpserver:
def __init__(self, ip, port, my_spawn):
self.ip = ip
self.port = port
self.server = socket.socket()
self.server.bind((self.ip, self.port))
self.server.listen(5)
self.spawn = my_spawn # 儲存spawn本地
def wait_accept(self):
while 1:
conn, addr = self.server.accept()
self.spawn(self.handle_request, conn) # 檢測 handle_request的io
def handlewww.cppcns.com_request(self, conn):
while 1:
try:
data = conn.recv(1024)
if not data: break
conn.send(data.upper())
except exception as e:
print(e)
break
conn.close()
if __name__ == '__main__':
server = mytcpserver('127.0.0.1', 8888, spawn)
g1 = server.spawn(server.wait_accept) # 檢測wait_accept的io
g1.join() # 等待g1執行結束,即一直在迴圈檢測io
Python 使用socket編寫TCP服務程式
使用socket編寫tcp服務程式 by 鄭瑞國 1 引用網路模組socket 2 建立乙個tcp網路套接字s 3 繫結本機ip位址和指定埠號 4 開始監聽 5 接受連線 6 接受資訊 import socket 1 引用網路模組socket s socket.socket socket.af in...
Python實現Socket程式設計
socket又稱套接字,應用程式通過套接字向網路發出請求。應用程式通過套接字應答網路請求。使主機間或者一台計算機上的程序間可以通訊。服務端 import socket 匯入socket模組 s socket.socket 建立socket物件 s.bind 127.0.0.1 6666 繫結埠 s....
Python實現簡易Socket
客戶端 向服務端傳送資訊和接收服務端返回的資訊 import socket flag true client socket.socket client.connect localhost 8080 連線服務埠 while flag msg input strip 獲取要傳送的資訊 if len ms...