
- Python - 網路程式設計
- Python - 網路入門
- Python - 網路環境
- Python - 網際網路協議
- Python - IP 地址
- Python - DNS 查詢
- Python - 路由
- Python - HTTP 請求
- Python - HTTP 響應
- Python - HTTP 頭部
- Python - 自定義 HTTP 請求
- Python - 請求狀態碼
- Python - HTTP 認證
- Python - HTTP 資料下載
- Python - 連線重用
- Python - 網路介面
- Python - 套接字程式設計
- Python - HTTP 客戶端
- Python - HTTP 伺服器
- Python - 構建 URL
- Python - Web 表單提交
- Python - 資料庫和 SQL
- Python - Telnet
- Python - 電子郵件
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web 伺服器
- Python - 上傳資料
- Python - 代理伺服器
- Python - 目錄列表
- Python - 遠端過程呼叫
- Python - RPC JSON 伺服器
- Python - 谷歌地圖
- Python - RSS Feed
Python - 套接字程式設計
Python 提供了兩個級別的網路服務訪問。在低級別,您可以訪問底層作業系統的基本套接字支援,這使您可以為面向連線和無連線協議實現客戶端和伺服器。
Python 還具有提供對特定應用程式級網路協議(例如 FTP、HTTP 等)的更高級別訪問的庫。
套接字是雙向通訊通道的端點。套接字可以在程序內、同一臺機器上的程序之間或不同大陸上的程序之間進行通訊。我們使用 Python 中的 socket 模組來建立和使用套接字。
套接字有自己的詞彙表 -
序號 | 術語及描述 |
---|---|
1 | 域 用作傳輸機制的協議族。這些值是常量,例如 AF_INET、PF_INET、PF_UNIX、PF_X25 等。 |
2 | 型別 兩個端點之間通訊的型別,通常對於面向連線的協議為 SOCK_STREAM,對於無連線的協議為 SOCK_DGRAM。 |
3 | 協議 通常為零,這可用於識別域和型別內協議的變體。 |
4 | 主機名 網路介面的識別符號 -
|
5 | 埠 每個伺服器都在一個或多個埠上偵聽客戶端的呼叫。埠可以是 Fixnum 埠號、包含埠號的字串或服務的名稱。 |
socket 模組
要建立套接字,必須使用 socket 模組中提供的 socket.socket() 函式,其通用語法如下 -
s = socket.socket (socket_family, socket_type, protocol=0)
以下是引數的描述 -
socket_family - 這是 AF_UNIX 或 AF_INET,如前所述。
socket_type - 這是 SOCK_STREAM 或 SOCK_DGRAM。
protocol - 通常省略,預設為 0。
擁有 socket 物件後,就可以使用所需函式建立客戶端或伺服器程式。
伺服器套接字方法
序號 | 方法及描述 |
---|---|
1 | s.bind() 此方法將地址(主機名、埠號對)繫結到套接字。 |
2 | s.listen() 此方法設定並啟動 TCP 偵聽器。 |
3 | s.accept() 此方法被動接受 TCP 客戶端連線,等待連線到達(阻塞)。 |
客戶端套接字方法
序號 | 方法及描述 |
---|---|
1 | s.connect() 此方法主動啟動 TCP 伺服器連線。 |
通用套接字方法
序號 | 方法及描述 |
---|---|
1 | s.recv() 此方法接收 TCP 訊息 |
2 | s.send() 此方法傳輸 TCP 訊息 |
3 | s.recvfrom() 此方法接收 UDP 訊息 |
4 | s.sendto() 此方法傳輸 UDP 訊息 |
5 | s.close() 此方法關閉套接字 |
6 | socket.gethostname() 返回主機名。 |
一個簡單的伺服器
要編寫 Internet 伺服器,我們使用 socket 模組中提供的 socket 函式來建立套接字物件。然後使用套接字物件呼叫其他函式來設定套接字伺服器。
現在呼叫 bind(hostname, port) 函式為給定主機上的服務指定一個 port。
接下來,呼叫返回物件的 accept 方法。此方法等待客戶端連線到您指定的埠,然後返回一個 connection 物件,該物件表示與該客戶端的連線。
#!/usr/bin/python # This is server.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr c.send('Thank you for connecting') c.close() # Close the connection
一個簡單的客戶端
讓我們編寫一個非常簡單的客戶端程式,該程式開啟到給定埠 12345 和給定主機的連線。使用 Python 的 socket 模組函式建立套接字客戶端非常簡單。
socket.connect(hosname, port ) 開啟到 hostname 上 port 的 TCP 連線。開啟套接字後,您可以像任何 IO 物件一樣從中讀取。完成後,請記住關閉它,就像關閉檔案一樣。
以下程式碼是一個非常簡單的客戶端,它連線到給定主機和埠,從套接字讀取任何可用資料,然後退出 -
#!/usr/bin/python # This is client.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close # Close the socket when done
現在在後臺執行此 server.py,然後執行上面的 client.py 以檢視結果。
# Following would start a server in background. $ python server.py & # Once server is started run client as follows: $ python client.py
這將產生以下結果 -
Got connection from ('127.0.0.1', 48437) Thank you for connecting
帶有公共 URL 的套接字
在下面的示例中,我們使用 socket 模組中的幾個方法來查詢伺服器的地址資訊和主機名詳細資訊。
import socket from pprint import pprint # get server address addrinfo = socket.getaddrinfo('tutorialspoint.com', 'www') pprint(addrinfo) # get server hostname print socket.gethostname()
當我們執行上述程式時,我們得到以下輸出 -
[(2, 1, 0, '', ('94.130.81.180', 80))] DESKTOP-JXYKQCPLP