日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python之socket编程

發布時間:2024/7/23 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python之socket编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


From :?http://www.cnblogs.com/aylin/p/5572104.html


1、socket

? ? ? ? socket起源于Unix,而Unix/Linux基本哲學之一就是“一切皆文件”,對于文件用【打開】【讀寫】【關閉】模式來操作。socket就是該模式的一個實現,socket即是一種特殊的文件,一些socket函數就是對其進行的操作(讀/寫IO、打開、關閉)
? ? ? ? 基本上,Socket 是任何一種計算機網絡通訊中最基礎的內容。例如當你在瀏覽器地址欄中輸入 http://www.cnblogs.com/ 時,你會打開一個套接字,然后連接到 http://www.cnblogs.com/ 并讀取響應的頁面然后然后顯示出來。而其他一些聊天客戶端如 gtalk 和 skype 也是類似。任何網絡通訊都是通過 Socket 來完成的。

Python 官方關于 Socket 的函數請看?http://docs.python.org/library/socket.html

socket和file的區別:

  1、file模塊是針對某個指定文件進行【打開】【讀寫】【關閉】

  2、socket模塊是針對 服務器端 和 客戶端Socket 進行【打開】【讀寫】【關閉】


python 編寫server的步驟:

1. 第一步是創建socket對象。調用socket構造函數。如:socket = socket.socket( family, type )family參數代表地址家族,可為AF_INET或AF_UNIX。AF_INET家族包括Internet地址,AF_UNIX家族用于同一臺機器上的進程間通信。type參數代表套接字類型,可為SOCK_STREAM(流套接字)和SOCK_DGRAM(數據報套接字)。 2. 第二步是將socket綁定到指定地址。這是通過socket對象的bind方法來實現的:socket.bind( address )由AF_INET所創建的套接字,address地址必須是一個雙元素元組,格式是(host,port)。host代表主機,port代表端口號。如果端口號正在使用、主機名不正確或端口已被保留,bind方法將引發socket.error異常。 3. 第三步是使用socket套接字的listen方法接收連接請求。socket.listen( backlog )backlog指定最多允許多少個客戶連接到服務器。它的值至少為1。收到連接請求后,這些請求需要排隊,如果隊列滿,就拒絕請求。 4. 第四步是服務器套接字通過socket的accept方法等待客戶請求一個連接。connection, address = socket.accept()調用accept方法時,socket會時入“waiting”狀態。客戶請求連接時,方法建立連接并返回服務器。accept方法返回一個含有兩個元素的元組(connection,address)。第一個元素connection是新的socket對象,服務器必須通過它與客戶通信;第二個元素 address是客戶的Internet地址。 5. 第五步是處理階段。服務器和客戶端通過send和recv方法通信(傳輸 數據)。服務器調用send,并采用字符串形式向客戶發送信息。send方法返回已發送的字符個數。服務器使用recv方法從客戶接收信息。調用recv 時,服務器必須指定一個整數,它對應于可通過本次方法調用來接收的最大數據量。recv方法在接收數據時會進入“blocked”狀態,最后返回一個字符串,用它表示收到的數據。如果發送的數據量超過了recv所允許的,數據會被截短。多余的數據將緩沖于接收端。以后調用recv時,多余的數據會從緩沖區 刪除(以及自上次調用recv以來,客戶可能發送的其它任何數據)。 6. 傳輸結束,服務器調用socket的close方法關閉連接。

python編寫client的步驟:

1. 創建一個socket以連接服務器:socket = socket.socket( family, type ) 2. 使用socket的connect方法連接服務器。對于AF_INET家族,連接格式如下:socket.connect( (host,port) )host代表服務器主機名或IP,port代表服務器進程所綁定的端口號。如連接成功,客戶就可通過套接字與服務器通信,如果連接失敗,會引發socket.error異常。 3. 處理階段,客戶和服務器將通過send方法和recv方法通信。 4. 傳輸結束,客戶通過調用socket的close方法關閉連接。

# server.py if __name__ == '__main__':import socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.bind(('localhost', 8001))sock.listen(5)while True:connection,address = sock.accept()try:connection.settimeout(5)buf = connection.recv(1024)if buf == '1':connection.send('welcome to server!')else:connection.send('please go out!')except socket.timeout:print 'time out'connection.close()# client.py if __name__ == '__main__':import socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.connect(('localhost', 8001))import timetime.sleep(2)sock.send('1')print sock.recv(1024)sock.close()

Python 提供了兩個基本的 socket 模塊。

? ?第一個是 Socket,它提供了標準的 BSD Sockets API。

? ?第二個是 SocketServer, 它提供了服務器中心類,可以簡化網絡服務器的開發。

下面講的是Socket模塊功能

1、Socket?類型

套接字格式:

socket(family,type[,protocal])?使用給定的地址族、套接字類型、協議編號(默認為0)來創建套接字。

socket類型

描述

socket.AF_UNIX

只能夠用于單一的Unix系統進程間通信

socket.AF_INET

服務器之間網絡通信

socket.AF_INET6

IPv6

socket.SOCK_STREAM

流式socket , for TCP

socket.SOCK_DGRAM

數據報式socket , for UDP

socket.SOCK_RAW

原始套接字,普通的套接字無法處理ICMPIGMP等網絡報文,而SOCK_RAW可以;其次,SOCK_RAW也可以處理特殊的IPv4報文;此外,利用原始套接字,可以通過IP_HDRINCL套接字選項由用戶構造IP頭。

socket.SOCK_SEQPACKET

可靠的連續數據包服務

創建TCP Socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

創建UDP Socket

s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)


2、Socket?函數

注意點:

1TCP發送數據時,已建立好TCP連接,所以不需要指定地址。UDP是面向無連接的,每次發送要指定是發給誰。

2)服務端與客戶端不能直接發送列表,元組,字典。需要字符串化repr(data)

socket函數

描述

服務端socket函數

s.bind(address)

將套接字綁定到地址,?AF_INET,以元組(host,port)的形式表示地址.

s.listen(backlog)

開始監聽TCP傳入連接。backlog指定在拒絕連接之前,操作系統可以掛起的最大連接數量。該值至少為1,大部分應用程序設為5就可以了。

s.accept()

接受TCP連接并返回(conn,address,其中conn是新的套接字對象,可以用來接收和發送數據。address是連接客戶端的地址。

客戶端socket函數

s.connect(address)

連接到address處的套接字。一般address的格式為元組(hostname,port),如果連接出錯,返回socket.error錯誤。

s.connect_ex(adddress)

功能與connect(address)相同,但是成功返回0,失敗返回errno的值。

公共socket函數

s.recv(bufsize[,flag])

接受TCP套接字的數據。數據以字符串形式返回,bufsize指定要接收的最大數據量。flag提供有關消息的其他信息,通常可以忽略。

s.send(string[,flag])

發送TCP數據。將string中的數據發送到連接的套接字。返回值是要發送的字節數量,該數量可能小于string的字節大小。

s.sendall(string[,flag])

完整發送TCP數據。將string中的數據發送到連接的套接字,但在返回之前會嘗試發送所有數據。成功返回None,失敗則拋出異常。

s.recvfrom(bufsize[.flag])

接受UDP套接字的數據。與recv()類似,但返回值是(data,address)。其中data是包含接收數據的字符串,address是發送數據的套接字地址。

s.sendto(string[,flag],address)

發送UDP數據。將數據發送到套接字,address是形式為(ipaddrport)的元組,指定遠程地址。返回值是發送的字節數。

s.close()

關閉套接字。

s.getpeername()

返回連接套接字的遠程地址。返回值通常是元組(ipaddr,port)。

s.getsockname()

返回套接字自己的地址。通常是一個元組(ipaddr,port)

s.setsockopt(level,optname,value)

設置給定套接字選項的值。

s.getsockopt(level,optname[.buflen])

返回套接字選項的值。

s.settimeout(timeout)

設置套接字操作的超時期,timeout是一個浮點數,單位是秒。值為None表示沒有超時期。一般,超時期應該在剛創建套接字時設置,因為它們可能用于連接的操作(如connect()

s.gettimeout()

返回當前超時期的值,單位是秒,如果沒有設置超時期,則返回None

s.fileno()

返回套接字的文件描述符。

s.setblocking(flag)

如果flag0,則將套接字設為非阻塞模式,否則將套接字設為阻塞模式(默認值)。非阻塞模式下,如果調用recv()沒有發現任何數據,或send()調用無法立即發送數據,那么將引起socket.error異常。

s.makefile()

創建一個與該套接字相關連的文件

3、socket編程思路

TCP服務端:

1?創建套接字,綁定套接字到本地IP與端口

? ?#?socket.socket(socket.AF_INET,socket.SOCK_STREAM)?, s.bind()

2?開始監聽連接?? ? ? ? ? ? ? ? ? #s.listen()

3?進入循環,不斷接受客戶端的連接請求?? ? ? ? ? ? ?#s.accept()

4?然后接收傳來的數據,并發送給對方數據?? ? ? ? #s.recv() , s.sendall()

5?傳輸完畢后,關閉套接字?? ? ? ? ? ? ? ? ? ? #s.close()


TCP客戶端:

1?創建套接字,連接遠端地址

? ? ? ?#?socket.socket(socket.AF_INET,socket.SOCK_STREAM)?, s.connect()

2?連接后發送數據和接收數據?? ? ? ? ?# s.sendall(), s.recv()

3?傳輸完畢后,關閉套接字?? ? ? ? ?#s.close()

4、Socket編程之服務端代碼:

# Server.py #!/usr/bin/python import socket #socket模塊 import commands #執行系統命令模塊 HOST='10.0.0.245' PORT=50007 s= socket.socket(socket.AF_INET,socket.SOCK_STREAM) #定義socket類型,網絡通信,TCP s.bind((HOST,PORT)) #套接字綁定的IP與端口 s.listen(1) #開始TCP監聽 while 1:conn,addr=s.accept() #接受TCP連接,并返回新的套接字與IP地址print'Connected by',addr #輸出客戶端的IP地址while 1:data=conn.recv(1024) #把接收的數據實例化cmd_status,cmd_result=commands.getstatusoutput(data) #commands.getstatusoutput執行系統命令(即shell命令),返回兩個結果,第一個是狀態,成功則為0,第二個是執行成功或失敗的輸出信息if len(cmd_result.strip()) ==0: #如果輸出結果長度為0,則告訴客戶端完成。此用法針對于創建文件或目錄,創建成功不會有輸出信息conn.sendall('Done.')else:conn.sendall(cmd_result) #否則就把結果發給對端(即客戶端) conn.close() #關閉連接# Client.py #!/usr/bin/python import socket HOST='10.0.0.245' PORT=50007 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #定義socket類型,網絡通信,TCP s.connect((HOST,PORT)) #要連接的IP與端口 while 1:cmd=raw_input("Please input cmd:") #與人交互,輸入命令s.sendall(cmd) #把命令發送給對端data=s.recv(1024) #把接收的數據定義為變量print data #輸出變量 s.close() #關閉連接

socket更多功能

def bind(self, address): # real signature unknown; restored from __doc__"""bind(address)Bind the socket to a local address. For IP sockets, the address is apair (host, port); the host must refer to the local host. For raw packetsockets the address is a tuple (ifname, proto [,pkttype [,hatype]])""" '''將套接字綁定到本地地址。是一個IP套接字的地址對(主機、端口),主機必須參考本地主機。'''passdef close(self): # real signature unknown; restored from __doc__"""close()Close the socket. It cannot be used after this call."""'''關閉socket'''passdef connect(self, address): # real signature unknown; restored from __doc__"""connect(address)Connect the socket to a remote address. For IP sockets, the addressis a pair (host, port)."""'''將套接字連接到遠程地址。IP套接字的地址'''passdef connect_ex(self, address): # real signature unknown; restored from __doc__"""connect_ex(address) -> errnoThis is like connect(address), but returns an error code (the errno value)instead of raising an exception when an error occurs."""passdef detach(self): # real signature unknown; restored from __doc__"""detach()Close the socket object without closing the underlying file descriptor.The object cannot be used after this call, but the file descriptorcan be reused for other purposes. The file descriptor is returned.""" '''關閉套接字對象沒有關閉底層的文件描述符。'''passdef fileno(self): # real signature unknown; restored from __doc__"""fileno() -> integerReturn the integer file descriptor of the socket."""'''返回整數的套接字的文件描述符。'''return 0def getpeername(self): # real signature unknown; restored from __doc__"""getpeername() -> address infoReturn the address of the remote endpoint. For IP sockets, the addressinfo is a pair (hostaddr, port)."""'''返回遠程端點的地址。IP套接字的地址'''passdef getsockname(self): # real signature unknown; restored from __doc__"""getsockname() -> address infoReturn the address of the local endpoint. For IP sockets, the addressinfo is a pair (hostaddr, port)."""'''返回遠程端點的地址。IP套接字的地址'''passdef getsockopt(self, level, option, buffersize=None): # real signature unknown; restored from __doc__"""getsockopt(level, option[, buffersize]) -> valueGet a socket option. See the Unix manual for level and option.If a nonzero buffersize argument is given, the return value is astring of that length; otherwise it is an integer."""'''得到一個套接字選項'''passdef gettimeout(self): # real signature unknown; restored from __doc__"""gettimeout() -> timeoutReturns the timeout in seconds (float) associated with socket operations. A timeout of None indicates that timeouts on socket operations are disabled."""'''返回的超時秒數(浮動)與套接字相關聯'''return timeoutdef ioctl(self, cmd, option): # real signature unknown; restored from __doc__"""ioctl(cmd, option) -> longControl the socket with WSAIoctl syscall. Currently supported 'cmd' values areSIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."""return 0def listen(self, backlog=None): # real signature unknown; restored from __doc__"""listen([backlog])Enable a server to accept connections. If backlog is specified, it must beat least 0 (if it is lower, it is set to 0); it specifies the number ofunaccepted connections that the system will allow before refusing newconnections. If not specified, a default reasonable value is chosen."""'''使服務器能夠接受連接。'''passdef recv(self, buffersize, flags=None): # real signature unknown; restored from __doc__"""recv(buffersize[, flags]) -> dataReceive up to buffersize bytes from the socket. For the optional flagsargument, see the Unix manual. When no data is available, block untilat least one byte is available or until the remote end is closed. Whenthe remote end is closed and all data is read, return the empty string.""" '''當沒有數據可用,阻塞,直到至少一個字節是可用的或遠程結束之前關閉。'''passdef recvfrom(self, buffersize, flags=None): # real signature unknown; restored from __doc__"""recvfrom(buffersize[, flags]) -> (data, address info)Like recv(buffersize, flags) but also return the sender's address info."""passdef recvfrom_into(self, buffer, nbytes=None, flags=None): # real signature unknown; restored from __doc__"""recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."""passdef recv_into(self, buffer, nbytes=None, flags=None): # real signature unknown; restored from __doc__"""recv_into(buffer, [nbytes[, flags]]) -> nbytes_readA version of recv() that stores its data into a buffer rather than creating a new string. Receive up to buffersize bytes from the socket. If buffersize is not specified (or 0), receive up to the size available in the given buffer.See recv() for documentation about the flags."""passdef send(self, data, flags=None): # real signature unknown; restored from __doc__"""send(data[, flags]) -> countSend a data string to the socket. For the optional flagsargument, see the Unix manual. Return the number of bytessent; this may be less than len(data) if the network is busy."""'''發送一個數據字符串到套接字。'''passdef sendall(self, data, flags=None): # real signature unknown; restored from __doc__"""sendall(data[, flags])Send a data string to the socket. For the optional flagsargument, see the Unix manual. This calls send() repeatedlyuntil all data is sent. If an error occurs, it's impossibleto tell how much data has been sent."""'''發送一個數據字符串到套接字,直到所有數據發送完成'''passdef sendto(self, data, flags=None, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """sendto(data[, flags], address) -> countLike send(data, flags) but allows specifying the destination address.For IP sockets, the address is a pair (hostaddr, port)."""passdef setblocking(self, flag): # real signature unknown; restored from __doc__"""setblocking(flag)Set the socket to blocking (flag is true) or non-blocking (false).setblocking(True) is equivalent to settimeout(None);setblocking(False) is equivalent to settimeout(0.0).""" '''是否阻塞(默認True),如果設置False,那么accept和recv時一旦無數據,則報錯。'''passdef setsockopt(self, level, option, value): # real signature unknown; restored from __doc__"""setsockopt(level, option, value)Set a socket option. See the Unix manual for level and option.The value argument can either be an integer or a string."""passdef settimeout(self, timeout): # real signature unknown; restored from __doc__"""settimeout(timeout)Set a timeout on socket operations. 'timeout' can be a float,giving in seconds, or None. Setting a timeout of None disablesthe timeout feature and is equivalent to setblocking(1).Setting a timeout of zero is the same as setblocking(0)."""passdef share(self, process_id): # real signature unknown; restored from __doc__"""share(process_id) -> bytesShare the socket with another process. The target process idmust be provided and the resulting bytes object passed to the targetprocess. There the shared socket can be instantiated by callingsocket.fromshare()."""return b""def shutdown(self, flag): # real signature unknown; restored from __doc__"""shutdown(flag)Shut down the reading side of the socket (flag == SHUT_RD), the writing sideof the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR)."""passdef _accept(self): # real signature unknown; restored from __doc__"""_accept() -> (integer, address info)Wait for an incoming connection. Return a new socket file descriptorrepresenting the connection, and the address of the client.For IP sockets, the address info is a pair (hostaddr, port)."""pass 注:擼主知道大家懶,所以把全部功能的中文標記在每個功能的下面啦。下面擼主列一些經常用到的吧

sk.bind(address)
  s.bind(address) 將套接字綁定到地址。address地址的格式取決于地址族。在AF_INET下,以元組(host,port)的形式表示地址。
sk.listen(backlog)
  開始監聽傳入連接。backlog指定在拒絕連接之前,可以掛起的最大連接數量。backlog等于5,表示內核已經接到了連接請求,但服務器還沒有調用accept進行處理的連接個數最大為5。這個值不能無限大,因為要在內核中維護連接隊列
sk.setblocking(bool)
  是否阻塞(默認True),如果設置False,那么accept和recv時一旦無數據,則報錯。
sk.accept()
  接受連接并返回(conn,address),其中conn是新的套接字對象,可以用來接收和發送數據。address是連接客戶端的地址。
  接收TCP 客戶的連接(阻塞式)等待連接的到來
sk.connect(address)
  連接到address處的套接字。一般,address的格式為元組(hostname,port),如果連接出錯,返回socket.error錯誤。
sk.connect_ex(address)
  同上,只不過會有返回值,連接成功時返回 0 ,連接失敗時候返回編碼,例如:10061
sk.close()
  關閉套接字
sk.recv(bufsize[,flag])
  接受套接字的數據。數據以字符串形式返回,bufsize指定最多可以接收的數量。flag提供有關消息的其他信息,通常可以忽略。
sk.recvfrom(bufsize[.flag])
  與recv()類似,但返回值是(data,address)。其中data是包含接收數據的字符串,address是發送數據的套接字地址。
sk.send(string[,flag])
  將string中的數據發送到連接的套接字。返回值是要發送的字節數量,該數量可能小于string的字節大小。即:可能未將指定內容全部發送。
sk.sendall(string[,flag])
  將string中的數據發送到連接的套接字,但在返回之前會嘗試發送所有數據。成功返回None,失敗則拋出異常。內部通過遞歸調用send,將所有內容發送出去。
sk.sendto(string[,flag],address)
  將數據發送到套接字,address是形式為(ipaddr,port)的元組,指定遠程地址。返回值是發送的字節數。該函數主要用于UDP協議。
sk.settimeout(timeout)
  設置套接字操作的超時期,timeout是一個浮點數,單位是秒。值為None表示沒有超時期。一般,超時期應該在剛創建套接字時設置,因為它們可能用于連接的操作(如 client 連接最多等待5s )
sk.getpeername()
  返回連接套接字的遠程地址。返回值通常是元組(ipaddr,port)。
sk.getsockname()
  返回套接字自己的地址。通常是一個元組(ipaddr,port)
sk.fileno()
  套接字的文件描述符

TCP:

案例一:機器人聊天

import socketserver 服務端class Myserver(socketserver.BaseRequestHandler):def handle(self):conn = self.requestconn.sendall(bytes("你好,我是機器人",encoding="utf-8"))while True:ret_bytes = conn.recv(1024)ret_str = str(ret_bytes,encoding="utf-8")if ret_str == "q":breakconn.sendall(bytes(ret_str+"你好我好大家好",encoding="utf-8"))if __name__ == "__main__":server = socketserver.ThreadingTCPServer(("127.0.0.1",8080),Myserver)server.serve_forever()客戶端import socketobj = socket.socket()obj.connect(("127.0.0.1",8080))ret_bytes = obj.recv(1024) ret_str = str(ret_bytes,encoding="utf-8") print(ret_str)while True:inp = input("你好請問您有什么問題? \n >>>")if inp == "q":obj.sendall(bytes(inp,encoding="utf-8"))breakelse:obj.sendall(bytes(inp, encoding="utf-8"))ret_bytes = obj.recv(1024)ret_str = str(ret_bytes,encoding="utf-8")print(ret_str)

案例二 上傳文件

服務端import socketsk = socket.socket()sk.bind(("127.0.0.1",8080)) sk.listen(5)while True:conn,address = sk.accept()conn.sendall(bytes("歡迎光臨我愛我家",encoding="utf-8"))size = conn.recv(1024)size_str = str(size,encoding="utf-8")file_size = int(size_str)conn.sendall(bytes("開始傳送", encoding="utf-8"))has_size = 0f = open("db_new.jpg","wb")while True:if file_size == has_size:breakdate = conn.recv(1024)f.write(date)has_size += len(date)f.close()客戶端import socket import osobj = socket.socket()obj.connect(("127.0.0.1",8080))ret_bytes = obj.recv(1024) ret_str = str(ret_bytes,encoding="utf-8") print(ret_str)size = os.stat("yan.jpg").st_size obj.sendall(bytes(str(size),encoding="utf-8"))obj.recv(1024)with open("yan.jpg","rb") as f:for line in f:obj.sendall(line)

UDP:

udp傳輸

import socket ip_port = ('127.0.0.1',9999) sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0) sk.bind(ip_port)while True:data = sk.recv(1024)print dataimport socket ip_port = ('127.0.0.1',9999)sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0) while True:inp = input('數據:').strip()if inp == 'exit':breaksk.sendto(bytes(inp,encoding = "utf-8"),ip_port)sk.close()

WEB服務應用:

#!/usr/bin/env python #coding:utf-8 import socketdef handle_request(client):buf = client.recv(1024)client.send("HTTP/1.1 200 OK\r\n\r\n")client.send("Hello, World")def main():sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.bind(('localhost',8080))sock.listen(5)while True:connection, address = sock.accept()handle_request(connection)connection.close()if __name__ == '__main__':main()


2、IO多路復用

I/O(input/output),即輸入/輸出端口。每個設備都會有一個專用的I/O地址,用來處理自己的輸入輸出信息。首先什么是I/O:I/O分為磁盤io和網絡io,這里說的是網絡io
IO多路復用:I/O多路復用指:通過一種機制,可以監視多個描述符(socket),一旦某個描述符就緒(一般是讀就緒或者寫就緒),能夠通知程序進行相應的讀寫操作。
Linux中的 select,poll,epoll 都是IO多路復用的機制。
Linux下網絡I/O使用socket套接字來通信,普通I/O模型只能監聽一個socket,而I/O多路復用可同時監聽多個socket.
I/O多路復用避免阻塞在io上,原本為多進程或多線程來接收多個連接的消息變為單進程或單線程保存多個socket的狀態后輪詢處理.

Python中有一個select模塊,其中提供了:select、poll、epoll三個方法,分別調用系統的 select,poll,epoll 從而實現IO多

Windows Python: 提供: select Mac Python: 提供: select Linux Python: 提供: select、poll、epoll

對于select模塊操作的方法:

句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超時時間)參數: 可接受四個參數(前三個必須) 返回值:三個列表select方法用來監視文件句柄,如果句柄發生變化,則獲取該句柄。 1、當 參數1 序列中的句柄發生可讀時(accetp和read),則獲取發生變化的句柄并添加到 返回值1 序列中 2、當 參數2 序列中含有句柄時,則將該序列中所有的句柄添加到 返回值2 序列中 3、當 參數3 序列中的句柄發生錯誤時,則將該發生錯誤的句柄添加到 返回值3 序列中 4、當 超時時間 未設置,則select會一直阻塞,直到監聽的句柄發生變化 5、當 超時時間 = 1時,那么如果監聽的句柄均無任何變化,則select會阻塞 1 秒,之后返回三個空列表,如果監聽的句柄有變化,則直接執行。

利用select監聽終端操作實例

import socket import selectsk1 = socket.socket() sk1.bind(("127.0.0.1",8001)) sk1.listen()sk2 = socket.socket() sk2.bind(("127.0.0.1",8002)) sk2.listen()sk3 = socket.socket() sk3.bind(("127.0.0.1",8003)) sk3.listen()li = [sk1,sk2,sk3]while True:r_list,w_list,e_list = select.select(li,[],[],1) # r_list可變化的for line in r_list: conn,address = line.accept()conn.sendall(bytes("Hello World !",encoding="utf-8"))

利用select實現偽同時處理多個Socket客戶端請求

服務端: sk1 = socket.socket() sk1.bind(("127.0.0.1",8001)) sk1.listen()inpu = [sk1,]while True:r_list,w_list,e_list = select.select(inpu,[],[],1)for sk in r_list:if sk == sk1:conn,address = sk.accept()inpu.append(conn)else:try:ret = str(sk.recv(1024),encoding="utf-8")sk.sendall(bytes(ret+"hao",encoding="utf-8"))except Exception as ex:inpu.remove(sk)客戶端 import socketobj = socket.socket()obj.connect(('127.0.0.1',8001))while True:inp = input("Please(q\退出):\n>>>")obj.sendall(bytes(inp,encoding="utf-8"))if inp == "q":breakret = str(obj.recv(1024),encoding="utf-8")print(ret)

利用select實現偽同時處理多個Socket客戶端請求讀寫分離

服務端: import socket sk1 = socket.socket() sk1.bind(("127.0.0.1",8001)) sk1.listen() inputs = [sk1] import select message_dic = {} outputs = [] while True:r_list, w_list, e_list = select.select(inputs,[],inputs,1)print("正在監聽的socket對象%d" % len(inputs))print(r_list)for sk1_or_conn in r_list:if sk1_or_conn == sk1:conn,address = sk1_or_conn.accept()inputs.append(conn)message_dic[conn] = []else:try:data_bytes = sk1_or_conn.recv(1024)data_str = str(data_bytes,encoding="utf-8")sk1_or_conn.sendall(bytes(data_str+"好",encoding="utf-8"))except Exception as ex:inputs.remove(sk1_or_conn)else:data_str = str(data_bytes,encoding="utf-8")message_dic[sk1_or_conn].append(data_str)outputs.append(sk1_or_conn)for conn in w_list:recv_str = message_dic[conn][0]del message_dic[conn][0]conn.sendall(bytes(recv_str+"好",encoding="utf-8"))for sk in e_list:inputs.remove(sk)客戶端: import socketobj = socket.socket()obj.connect(('127.0.0.1',8001))while True:inp = input("Please(q\退出):\n>>>")obj.sendall(bytes(inp,encoding="utf-8"))if inp == "q":breakret = str(obj.recv(1024),encoding="utf-8")print(ret)


3、socketserver


SocketServer內部使用 IO多路復用 以及 “多線程” 和 “多進程” ,從而實現并發處理多個客戶端請求的Socket服務端。即:每個客戶端請求連接到服務器時,Socket服務端都會在服務器是創建一個“線程”或者“進程” 專門負責處理當前客戶端的所有請求。

ThreadingTCPServer

ThreadingTCPServer實現的Soket服務器內部會為每個client創建一個 “線程”,該線程用來和客戶端進行交互。

1、ThreadingTCPServer基礎

使用ThreadingTCPServer:

  • 創建一個繼承自 SocketServer.BaseRequestHandler 的類
  • 類中必須定義一個名稱為 handle 的方法
  • 啟動ThreadingTCPServer
# 服務端 import socketserverclass Myserver(socketserver.BaseRequestHandler):def handle(self):conn = self.requestconn.sendall(bytes("你好,我是機器人",encoding="utf-8"))while True:ret_bytes = conn.recv(1024)ret_str = str(ret_bytes,encoding="utf-8")if ret_str == "q":breakconn.sendall(bytes(ret_str+"你好我好大家好",encoding="utf-8"))if __name__ == "__main__":server = socketserver.ThreadingTCPServer(("127.0.0.1",8080),Myserver)server.serve_forever()############################ # 客戶端 import socketobj = socket.socket()obj.connect(("127.0.0.1",8080))ret_bytes = obj.recv(1024) ret_str = str(ret_bytes,encoding="utf-8") print(ret_str)while True:inp = input("你好請問您有什么問題? \n >>>")if inp == "q":obj.sendall(bytes(inp,encoding="utf-8"))breakelse:obj.sendall(bytes(inp, encoding="utf-8"))ret_bytes = obj.recv(1024)ret_str = str(ret_bytes,encoding="utf-8")print(ret_str)

2、ThreadingTCPServer源碼剖析

ThreadingTCPServer的類圖關系如下:


內部調用流程為:

  • 啟動服務端程序
  • 執行 TCPServer.__init__ 方法,創建服務端Socket對象并綁定 IP 和 端口
  • 執行 BaseServer.__init__ 方法,將自定義的繼承自SocketServer.BaseRequestHandler 的類 MyRequestHandle賦值給?self.RequestHandlerClass
  • 執行 BaseServer.server_forever 方法,While 循環一直監聽是否有客戶端請求到達 ...
  • 當客戶端連接到達服務器
  • 執行 ThreadingMixIn.process_request 方法,創建一個 “線程” 用來處理請求
  • 執行?ThreadingMixIn.process_request_thread 方法
  • 執行 BaseServer.finish_request 方法,執行?self.RequestHandlerClass() ?即:執行 自定義 MyRequestHandler 的構造方法(自動調用基類BaseRequestHandler的構造方法,在該構造方法中又會調用 MyRequestHandler的handle方法)

# Baseserver class BaseServer:"""Base class for server classes.Methods for the caller:- __init__(server_address, RequestHandlerClass)- serve_forever(poll_interval=0.5)- shutdown()- handle_request() # if you do not use serve_forever()- fileno() -> int # for select()Methods that may be overridden:- server_bind()- server_activate()- get_request() -> request, client_address- handle_timeout()- verify_request(request, client_address)- server_close()- process_request(request, client_address)- shutdown_request(request)- close_request(request)- handle_error()Methods for derived classes:- finish_request(request, client_address)Class variables that may be overridden by derived classes orinstances:- timeout- address_family- socket_type- allow_reuse_addressInstance variables:- RequestHandlerClass- socket"""timeout = Nonedef __init__(self, server_address, RequestHandlerClass):"""Constructor. May be extended, do not override."""self.server_address = server_addressself.RequestHandlerClass = RequestHandlerClassself.__is_shut_down = threading.Event()self.__shutdown_request = Falsedef server_activate(self):"""Called by constructor to activate the server.May be overridden."""passdef serve_forever(self, poll_interval=0.5):"""Handle one request at a time until shutdown.Polls for shutdown every poll_interval seconds. Ignoresself.timeout. If you need to do periodic tasks, do them inanother thread."""self.__is_shut_down.clear()try:while not self.__shutdown_request:# XXX: Consider using another file descriptor or# connecting to the socket to wake this up instead of# polling. Polling reduces our responsiveness to a# shutdown request and wastes cpu at all other times.r, w, e = _eintr_retry(select.select, [self], [], [],poll_interval)if self in r:self._handle_request_noblock()finally:self.__shutdown_request = Falseself.__is_shut_down.set()def shutdown(self):"""Stops the serve_forever loop.Blocks until the loop has finished. This must be called whileserve_forever() is running in another thread, or it willdeadlock."""self.__shutdown_request = Trueself.__is_shut_down.wait()# The distinction between handling, getting, processing and# finishing a request is fairly arbitrary. Remember:## - handle_request() is the top-level call. It calls# select, get_request(), verify_request() and process_request()# - get_request() is different for stream or datagram sockets# - process_request() is the place that may fork a new process# or create a new thread to finish the request# - finish_request() instantiates the request handler class;# this constructor will handle the request all by itselfdef handle_request(self):"""Handle one request, possibly blocking.Respects self.timeout."""# Support people who used socket.settimeout() to escape# handle_request before self.timeout was available.timeout = self.socket.gettimeout()if timeout is None:timeout = self.timeoutelif self.timeout is not None:timeout = min(timeout, self.timeout)fd_sets = _eintr_retry(select.select, [self], [], [], timeout)if not fd_sets[0]:self.handle_timeout()returnself._handle_request_noblock()def _handle_request_noblock(self):"""Handle one request, without blocking.I assume that select.select has returned that the socket isreadable before this function was called, so there should beno risk of blocking in get_request()."""try:request, client_address = self.get_request()except socket.error:returnif self.verify_request(request, client_address):try:self.process_request(request, client_address)except:self.handle_error(request, client_address)self.shutdown_request(request)def handle_timeout(self):"""Called if no new request arrives within self.timeout.Overridden by ForkingMixIn."""passdef verify_request(self, request, client_address):"""Verify the request. May be overridden.Return True if we should proceed with this request."""return Truedef process_request(self, request, client_address):"""Call finish_request.Overridden by ForkingMixIn and ThreadingMixIn."""self.finish_request(request, client_address)self.shutdown_request(request)def server_close(self):"""Called to clean-up the server.May be overridden."""passdef finish_request(self, request, client_address):"""Finish one request by instantiating RequestHandlerClass."""self.RequestHandlerClass(request, client_address, self)def shutdown_request(self, request):"""Called to shutdown and close an individual request."""self.close_request(request)def close_request(self, request):"""Called to clean up an individual request."""passdef handle_error(self, request, client_address):"""Handle an error gracefully. May be overridden.The default is to print a traceback and continue."""print '-'*40print 'Exception happened during processing of request from',print client_addressimport tracebacktraceback.print_exc() # XXX But this goes to stderr!print '-'*40

# TCP server class TCPServer(BaseServer):"""Base class for various socket-based server classes.Defaults to synchronous IP stream (i.e., TCP).Methods for the caller:- __init__(server_address, RequestHandlerClass, bind_and_activate=True)- serve_forever(poll_interval=0.5)- shutdown()- handle_request() # if you don't use serve_forever()- fileno() -> int # for select()Methods that may be overridden:- server_bind()- server_activate()- get_request() -> request, client_address- handle_timeout()- verify_request(request, client_address)- process_request(request, client_address)- shutdown_request(request)- close_request(request)- handle_error()Methods for derived classes:- finish_request(request, client_address)Class variables that may be overridden by derived classes orinstances:- timeout- address_family- socket_type- request_queue_size (only for stream sockets)- allow_reuse_addressInstance variables:- server_address- RequestHandlerClass- socket"""address_family = socket.AF_INETsocket_type = socket.SOCK_STREAMrequest_queue_size = 5allow_reuse_address = Falsedef __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):"""Constructor. May be extended, do not override."""BaseServer.__init__(self, server_address, RequestHandlerClass)self.socket = socket.socket(self.address_family,self.socket_type)if bind_and_activate:try:self.server_bind()self.server_activate()except:self.server_close()raisedef server_bind(self):"""Called by constructor to bind the socket.May be overridden."""if self.allow_reuse_address:self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)self.socket.bind(self.server_address)self.server_address = self.socket.getsockname()def server_activate(self):"""Called by constructor to activate the server.May be overridden."""self.socket.listen(self.request_queue_size)def server_close(self):"""Called to clean-up the server.May be overridden."""self.socket.close()def fileno(self):"""Return socket file number.Interface required by select()."""return self.socket.fileno()def get_request(self):"""Get the request and client address from the socket.May be overridden."""return self.socket.accept()def shutdown_request(self, request):"""Called to shutdown and close an individual request."""try:#explicitly shutdown. socket.close() merely releases#the socket and waits for GC to perform the actual close.request.shutdown(socket.SHUT_WR)except socket.error:pass #some platforms may raise ENOTCONN hereself.close_request(request)def close_request(self, request):"""Called to clean up an individual request."""request.close()

# ThreadingMixIn class ThreadingMixIn:"""Mix-in class to handle each request in a new thread."""# Decides how threads will act upon termination of the# main processdaemon_threads = Falsedef process_request_thread(self, request, client_address):"""Same as in BaseServer but as a thread.In addition, exception handling is done here."""try:self.finish_request(request, client_address)self.shutdown_request(request)except:self.handle_error(request, client_address)self.shutdown_request(request)def process_request(self, request, client_address):"""Start a new thread to process the request."""t = threading.Thread(target = self.process_request_thread,args = (request, client_address))t.daemon = self.daemon_threadst.start()
# SocketServer.BaseRequestHandler class BaseRequestHandler:"""Base class for request handler classes.This class is instantiated for each request to be handled. Theconstructor sets the instance variables request, client_addressand server, and then calls the handle() method. To implement aspecific service, all you need to do is to derive a class whichdefines a handle() method.The handle() method can find the request as self.request, theclient address as self.client_address, and the server (in case itneeds access to per-server information) as self.server. Since aseparate instance is created for each request, the handle() methodcan define arbitrary other instance variariables."""def __init__(self, request, client_address, server):self.request = requestself.client_address = client_addressself.server = serverself.setup()try:self.handle()finally:self.finish()def setup(self):passdef handle(self):passdef finish(self):pass

SocketServer的ThreadingTCPServer之所以可以同時處理請求得益于 select 和 Threading 兩個東西,其實本質上就是在服務器端為每一個客戶端創建一個線程,當前線程用來處理對應客戶端的請求,所以,可以支持同時n個客戶端鏈接(長連接)。



創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python之socket编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。