Python實現(xiàn)的服務(wù)器示例小結(jié)【單進程、多進程、多線程、非阻塞式】
本文實例講述了Python實現(xiàn)的服務(wù)器。分享給大家供大家參考,具體如下:
python - 單進程服務(wù)器
#coding=utf-8
from socket import *
#創(chuàng)建套接字
serSocket = socket(AF_INET, SOCK_STREAM)
#重復(fù)使用綁定信息
serSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
localAddr = ('', 7788)
#綁定端口ip
serSocket.bind(localAddr)
#監(jiān)聽
serSocket.listen(5)
while True:
print('---主進程,等待新客戶端的到來---')
newSocket,destAddr = serSocket.accept()
print('---主進程,接下來負(fù)責(zé)數(shù)據(jù)處理[%s]---'%str(destAddr))
try:
while True:
recvData = newSocket.recv(1024)
if len(recvData)>0:
print('recv[%s]:%s'%(str(destAddr),recvData))
else:
print('[%s]客戶端已經(jīng)關(guān)閉')
break
finally:
newSocket.close()
serSocket.close()
總結(jié)
同一時刻只能為一個客戶進行服務(wù),不能同時為多個客戶服務(wù)。
當(dāng)服務(wù)器為一個客戶端服務(wù)時,另外的客戶端發(fā)起了connect,只要服務(wù)器listen的隊列有空閑的位置,就會為這個新客戶端進行連接,并且客戶端可以發(fā)送數(shù)據(jù),但當(dāng)服務(wù)器為這個新客戶端服務(wù)時,可能一次性把所有數(shù)據(jù)接收完畢當(dāng)recv接收數(shù)據(jù)時,返回值為空,即沒有返回數(shù)據(jù),那么意味著客戶端已經(jīng)調(diào)用了close關(guān)閉了;因此服務(wù)器通過判斷recv接收數(shù)據(jù)是否為空 來判斷客戶端是否已經(jīng)下線。
python - 多進程服務(wù)器
#coding=utf-8
from socket import *
from multiprocessing import *
from time import sleep
#處理客戶端的請求并為其服務(wù)
def dealWithClient(newSocket,destAddr):
try:
while True:
recvData = newSocket.recv(1024)
if len(recvData) > 0:
print('recv[%s]:%s' % (str(destAddr), recvData))
else:
print('[%s]客戶端已經(jīng)關(guān)閉')
break
finally:
newSocket.close()
def main():
#創(chuàng)建套接字
serSocket = socket(AF_INET, SOCK_STREAM)
#重復(fù)使用綁定信息
serSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
localAddr = ('', 7788)
#綁定端口ip
serSocket.bind(localAddr)
#監(jiān)聽
serSocket.listen(5)
try:
while True:
print('---主進程,等待新客戶端的到來---')
newSocket,destAddr = serSocket.accept()
print('---主進程,接下來負(fù)責(zé)數(shù)據(jù)處理[%s]---'%str(destAddr))
client = Process(target=dealWithClient, args=(newSocket, destAddr))
client.start()
# 因為已經(jīng)向子進程中copy了一份(引用),并且父進程中這個套接字也沒有用處了
# 所以關(guān)閉
newSocket.close()
finally:
# 當(dāng)為所有的客戶端服務(wù)完之后再進行關(guān)閉,表示不再接收新的客戶端的鏈接
serSocket.close()
if __name__ == '__main__':
main()
總結(jié)
通過為每個客戶端創(chuàng)建一個進程的方式,能夠同時為多個客戶端進行服
務(wù)當(dāng)客戶端不是特別多的時候,這種方式還行,如果有成百上千個,就不
可取了,因為每次創(chuàng)建進程的過程需要消耗較多的資源。
python - 多線程服務(wù)器
#coding=utf-8
from socket import *
from threading import Thread
from time import sleep
# 處理客戶端的請求并執(zhí)行事情
def dealWithClient(newSocket,destAddr):
while True:
recvData = newSocket.recv(1024)
if len(recvData)>0:
print('recv[%s]:%s'%(str(destAddr), recvData))
else:
print('[%s]客戶端已經(jīng)關(guān)閉'%str(destAddr))
break
newSocket.close()
def main():
serSocket = socket(AF_INET, SOCK_STREAM)
serSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR , 1)
localAddr = ('', 7788)
serSocket.bind(localAddr)
serSocket.listen(5)
try:
while True:
print('-----主進程,,等待新客戶端的到來------')
newSocket,destAddr = serSocket.accept()
print('-----主進程,,接下來創(chuàng)建一個新的進程負(fù)責(zé)數(shù)據(jù)處理[%s]----'%str(destAddr))
client = Thread(target=dealWithClient, args=(newSocket,destAddr))
client.start()
#因為線程中共享這個套接字,如果關(guān)閉了會導(dǎo)致這個套接字不可用,
#但是此時在線程中這個套接字可能還在收數(shù)據(jù),因此不能關(guān)閉
#newSocket.close()
finally:
# 當(dāng)為所有的客戶端服務(wù)完之后再進行關(guān)閉,表示不再接收新的客戶端的鏈接
serSocket.close()
if __name__ == '__main__':
main()
單進程服務(wù)器-非堵塞模式
服務(wù)器:
#coding=utf-8
from socket import *
import time
# 用來存儲所有的新鏈接的socket
g_socketList = []
def main():
serSocket = socket(AF_INET, SOCK_STREAM)
serSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR , 1)
localAddr = ('', 7788)
serSocket.bind(localAddr)
#可以適當(dāng)修改listen中的值來看看不同的現(xiàn)象
serSocket.listen(1000)
#將套接字設(shè)置為?堵塞
#設(shè)置為?堵塞后,如果accept時,恰巧沒有客戶端connect,那么accept會
#產(chǎn)生一個異常,所以需要try來進行處理
serSocket.setblocking(False)
while True:
#?來測試
#time.sleep(0.5)
try:
newClientInfo = serSocket.accept()
except Exception as result:
pass
else:
print("一個新的客戶端到來:%s"%str(newClientInfo))
newClientInfo[0].setblocking(False)
g_socketList.append(newClientInfo)
# 用來存儲需要刪除的客戶端信息
needDelClientInfoList = []
for clientSocket,clientAddr in g_socketList:
try:
recvData = clientSocket.recv(1024)
if len(recvData)>0:
print('recv[%s]:%s'%(str(clientAddr), recvData))
else:
print('[%s]客戶端已經(jīng)關(guān)閉'%str(clientAddr))
clientSocket.close()
g_needDelClientInfoList.append((clientSocket,clientAddr))
except Exception as result:
pass
for needDelClientInfo in needDelClientInfoList:
g_socketList.remove(needDelClientInfo)
if __name__ == '__main__':
main()
客戶端:
#coding=utf-8
from socket import *
import random
import time
serverIp = input("請輸入服務(wù)器的ip:")
connNum = input("請輸入要鏈接服務(wù)器的次數(shù)(例如1000):")
g_socketList = []
for i in range(int(connNum)):
s = socket(AF_INET, SOCK_STREAM)
s.connect((serverIp, 7788))
g_socketList.append(s)
print(i)
while True:
for s in g_socketList:
s.send(str(random.randint(0,100)))
# 用來測試
#time.sleep(1)
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python接口自動化(十六)--參數(shù)關(guān)聯(lián)接口后傳(詳解)
這篇文章主要介紹了python接口自動化參數(shù)關(guān)聯(lián)接口,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
python函數(shù)裝飾器構(gòu)造和參數(shù)傳遞
這篇文章主要介紹了python函數(shù)裝飾器構(gòu)造和參數(shù)傳遞,下面通過一個小案例來簡單的理解什么是裝飾器,需要的小伙伴可以參考一下2022-03-03
python結(jié)合opencv實現(xiàn)人臉檢測與跟蹤
在Python下用起來OpenCV很爽,代碼很簡潔,很清晰易懂。使用的是Haar特征的分類器,訓(xùn)練之后得到的數(shù)據(jù)存在一個xml中。下面我們就來詳細談?wù)劇?/div> 2015-06-06最新評論

