socket基本流程
客戶端和服務端的交互
SocketServer實現異步多線程服務端
SocketServer上傳文件html
socket實現ftp服務器python
基本上,Socket 是任何一種計算機網絡通信中最基礎的內容。例如當你在瀏覽器地址欄中輸入URL 時,你會打開一個套接字,而後鏈接到 URL 並讀取響應的頁面而後而後顯示出來。而其餘一些聊天客戶端如 gtalk 和 skype 也是相似。任何網絡通信都是經過 Socket 來完成的。nginx
首先要作的就是建立一個 Socket,socket 的 socket 函數能夠實現,代碼以下:編程
1 #Socket client example in python 2 import socket #for sockets 3 4 #create an AF_INET, STREAM socket (TCP) 5 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 7 print 'Socket Created'
函數 socket.socket 建立了一個 Socket,並返回 Socket 的描述符可用於其餘 Socket 相關的函數。瀏覽器
上述代碼使用了下面兩個屬性來建立 Socket:服務器
地址簇 : AF_INET (IPv4)
類型: SOCK_STREAM (使用 TCP 傳輸控制協議)網絡
錯誤處理多線程
若是 socket 函數失敗了,python 將拋出一個名爲 socket.error 的異常,這個異常必須予以處理:併發
1 #handling errors in python socket programs 2 3 import socket #for sockets 4 import sys #for exit 5 6 try: 7 #create an AF_INET, STREAM socket (TCP) 8 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 except socket.error, msg: 10 print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1] 11 sys.exit(); 12 13 print 'Socket Created'
好了,假設你已經成功建立了 Socket,下一步該作什麼呢?接下來咱們將使用這個 Socket 來鏈接到服務器。app
注意
與 SOCK_STREAM 相對應的其餘類型是 SOCK_DGRAM 用於 UDP 通信協議,UDP 通信是非鏈接 Socket,在這篇文章中咱們只討論 SOCK_STREAM ,或者叫 TCP 。
鏈接到服務器須要服務器地址和端口號,這裏使用的是 www.oschina.net 和 80 端口。
首先獲取遠程主機的 IP 地址
鏈接到遠程主機以前,咱們須要知道它的 IP 地址,在 Python 中,獲取 IP 地址是很簡單的:
1 import socket #for sockets 2 import sys #for exit 3 4 try: 5 #create an AF_INET, STREAM socket (TCP) 6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 except socket.error, msg: 8 print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1] 9 sys.exit(); 10 11 print 'Socket Created' 12 13 host = 'www.oschina.net' 14 15 try: 16 remote_ip = socket.gethostbyname( host ) 17 18 except socket.gaierror: 19 #could not resolve 20 print 'Hostname could not be resolved. Exiting' 21 sys.exit() 22 23 print 'Ip address of ' + host + ' is ' + remote_ip
咱們已經有 IP 地址了,接下來須要指定要鏈接的端口。
代碼:
1 import socket #for sockets 2 import sys #for exit 3 4 try: 5 #create an AF_INET, STREAM socket (TCP) 6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 except socket.error, msg: 8 print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1] 9 sys.exit(); 10 11 print 'Socket Created' 12 13 host = 'www.oschina.net' 14 port = 80 15 16 try: 17 remote_ip = socket.gethostbyname( host ) 18 19 except socket.gaierror: 20 #could not resolve 21 print 'Hostname could not be resolved. Exiting' 22 sys.exit() 23 24 print 'Ip address of ' + host + ' is ' + remote_ip 25 26 #Connect to remote server 27 s.connect((remote_ip , port)) 28 29 print 'Socket Connected to ' + host + ' on ip ' + remote_ip
如今運行程序
1 $ python client.py 2 Socket Created 3 Ip address of www.oschina.net is 61.145.122.155 4 Socket Connected to www.oschina.net on ip 61.145.122.155
這段程序建立了一個 Socket 並進行鏈接,試試使用其餘一些不存在的端口(如81)會是怎樣?這個邏輯至關於構建了一個端口掃描器。
已經鏈接上了,接下來就是往服務器上發送數據。
免費提示
使用 SOCK_STREAM/TCP 套接字纔有「鏈接」的概念。鏈接意味着可靠的數據流通信機制,能夠同時有多個數據流。能夠想象成一個數據互不干擾的管道。另一個重要的提示是:數據包的發送和接收是有順序的。
其餘一些 Socket 如 UDP、ICMP 和 ARP 沒有「鏈接」的概念,它們是無鏈接通信,意味着你可從任何人或者給任何人發送和接收數據包。
sendall 函數用於簡單的發送數據,咱們來向 oschina 發送一些數據:
1 import socket #for sockets 2 import sys #for exit 3 4 try: 5 #create an AF_INET, STREAM socket (TCP) 6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 except socket.error, msg: 8 print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1] 9 sys.exit(); 10 11 print 'Socket Created' 12 13 host = 'www.oschina.net' 14 port = 80 15 16 try: 17 remote_ip = socket.gethostbyname( host ) 18 19 except socket.gaierror: 20 #could not resolve 21 print 'Hostname could not be resolved. Exiting' 22 sys.exit() 23 24 print 'Ip address of ' + host + ' is ' + remote_ip 25 26 #Connect to remote server 27 s.connect((remote_ip , port)) 28 29 print 'Socket Connected to ' + host + ' on ip ' + remote_ip 30 31 #Send some data to remote server 32 message = "GET / HTTP/1.1\r\n\r\n" 33 34 try : 35 #Set the whole string 36 s.sendall(message) 37 except socket.error: 38 #Send failed 39 print 'Send failed' 40 sys.exit() 41 42 print 'Message send successfully'
上述例子中,首先鏈接到目標服務器,而後發送字符串數據 "GET / HTTP/1.1\r\n\r\n" ,這是一個 HTTP 協議的命令,用來獲取網站首頁的內容。
接下來須要讀取服務器返回的數據。
recv 函數用於從 socket 接收數據:
1 #Socket client example in python 2 3 import socket #for sockets 4 import sys #for exit 5 6 #create an INET, STREAMing socket 7 try: 8 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 except socket.error: 10 print 'Failed to create socket' 11 sys.exit() 12 13 print 'Socket Created' 14 15 host = 'oschina.net'; 16 port = 80; 17 18 try: 19 remote_ip = socket.gethostbyname( host ) 20 21 except socket.gaierror: 22 #could not resolve 23 print 'Hostname could not be resolved. Exiting' 24 sys.exit() 25 26 #Connect to remote server 27 s.connect((remote_ip , port)) 28 29 print 'Socket Connected to ' + host + ' on ip ' + remote_ip 30 31 #Send some data to remote server 32 message = "GET / HTTP/1.1\r\nHost: oschina.net\r\n\r\n" 33 34 try : 35 #Set the whole string 36 s.sendall(message) 37 except socket.error: 38 #Send failed 39 print 'Send failed' 40 sys.exit() 41 42 print 'Message send successfully' 43 44 #Now receive data 45 reply = s.recv(4096) 46 47 print reply
下面是上述程序執行的結果:
1 $ python client.py 2 Socket Created 3 Ip address of oschina.net is 61.145.122. 4 Socket Connected to oschina.net on ip 61.145.122.155 5 Message send successfully 6 HTTP/1.1 301 Moved Permanently 7 Server: nginx 8 Date: Wed, 24 Oct 2012 13:26:46 GMT 9 Content-Type: text/html 10 Content-Length: 178 11 Connection: keep-alive 12 Keep-Alive: timeout=20 13 Location: http://www.oschina.net/
oschina.net 迴應了咱們所請求的 URL 的內容,很簡單。數據接收完了,能夠關閉 Socket 了。
close 函數用於關閉 Socket:
1 |
s.close() |
這就是了。
上述的示例中咱們學到了如何:
1. 建立 Socket
2. 鏈接到遠程服務器
3. 發送數據
4. 接收回應
當你用瀏覽器打開 www.oschina.net 時,其過程也是同樣。包含兩種類型,分別是客戶端和服務器,客戶端鏈接到服務器並讀取數據,服務器使用 Socket 接收進入的鏈接並提供數據。所以在這裏 www.oschina.net 是服務器端,而你的瀏覽器是客戶端。
接下來咱們開始在服務器端作點編碼。
服務器端編程主要包括下面幾步:
1. 打開 socket
2. 綁定到一個地址和端口
3. 偵聽進來的鏈接
4. 接受鏈接
5. 讀寫數據
咱們已經學習過如何打開 Socket 了,下面是綁定到指定的地址和端口上。
bind 函數用於將 Socket 綁定到一個特定的地址和端口,它須要一個相似 connect 函數所需的 sockaddr_in 結構體。
示例代碼:
1 import socket 2 import sys 3 4 HOST = '' # Symbolic name meaning all available interfaces 5 PORT = 8888 # Arbitrary non-privileged port 6 7 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 print 'Socket created' 9 10 try: 11 s.bind((HOST, PORT)) 12 except socket.error , msg: 13 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 14 sys.exit() 15 16 print 'Socket bind complete'
綁定完成後,就須要讓 Socket 開始偵聽鏈接。很顯然,你不能將兩個不一樣的 Socket 綁定到同一個端口之上。
綁定 Socket 以後就能夠開始偵聽鏈接,咱們須要將 Socket 變成偵聽模式。socket 的 listen 函數用於實現偵聽模式:
1 |
s.listen(10) |
2 |
print 'Socket now listening' |
listen 函數所需的參數成爲 backlog,用來控制程序忙時可保持等待狀態的鏈接數。這裏咱們傳遞的是 10,意味着若是已經有 10 個鏈接在等待處理,那麼第 11 個鏈接將會被拒絕。當檢查了 socket_accept 後這個會更加清晰。
示例代碼:
1 import socket 2 import sys 3 4 HOST = '' # Symbolic name meaning all available interfaces 5 PORT = 8888 # Arbitrary non-privileged port 6 7 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 print 'Socket created' 9 10 try: 11 s.bind((HOST, PORT)) 12 except socket.error , msg: 13 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 14 sys.exit() 15 16 print 'Socket bind complete' 17 18 s.listen(10) 19 print 'Socket now listening' 20 21 #wait to accept a connection - blocking call 22 conn, addr = s.accept() 23 24 #display client information 25 print 'Connected with ' + addr[0] + ':' + str(addr[1])
輸出
運行該程序將會顯示:
1 |
$ python server.py |
2 |
Socket created |
3 |
Socket bind complete |
4 |
Socket now listening |
如今這個程序開始等待鏈接進入,端口是 8888,請不要關閉這個程序,咱們來經過 telnet 程序來進行測試。
打開命令行窗口並輸入:
1 |
$ telnet localhost 8888 |
2 |
3 |
It will immediately show |
4 |
$ telnet localhost 8888 |
5 |
Trying 127.0.0.1... |
6 |
Connected to localhost. |
7 |
Escape character is '^]' . |
8 |
Connection closed by foreign host. |
而服務器端窗口顯示的是:
1 |
$ python server.py |
2 |
Socket created |
3 |
Socket bind complete |
4 |
Socket now listening |
5 |
Connected with 127.0.0.1:59954 |
咱們可看到客戶端已經成功鏈接到服務器。
上面例子咱們接收到鏈接並當即關閉,這樣的程序沒什麼實際的價值,鏈接創建後通常會有大量的事情須要處理,所以讓咱們來給客戶端作出點回應吧。
sendall 函數可經過 Socket 給客戶端發送數據:
1 import socket 2 import sys 3 4 HOST = '' # Symbolic name meaning all available interfaces 5 PORT = 8888 # Arbitrary non-privileged port 6 7 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 print 'Socket created' 9 10 try: 11 s.bind((HOST, PORT)) 12 except socket.error , msg: 13 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 14 sys.exit() 15 16 print 'Socket bind complete' 17 18 s.listen(10) 19 print 'Socket now listening' 20 21 #wait to accept a connection - blocking call 22 conn, addr = s.accept() 23 24 print 'Connected with ' + addr[0] + ':' + str(addr[1]) 25 26 #now keep talking with the client 27 data = conn.recv(1024) 28 conn.sendall(data) 29 30 conn.close() 31 s.close()
繼續運行上述代碼,而後打開另一個命令行窗口輸入下面命令:
1 |
$ telnet localhost 8888 |
2 |
Trying 127.0.0.1... |
3 |
Connected to localhost. |
4 |
Escape character is '^]' . |
5 |
happy |
6 |
happy |
7 |
Connection closed by foreign host. |
可看到客戶端接收到來自服務器端的迴應內容。
上面的例子仍是同樣,服務器端迴應後就當即退出了。而一些真正的服務器像 www.oschina.net 是一直在運行的,時刻接受鏈接請求。
也就是說服務器端應該一直處於運行狀態,不然就不能成爲「服務」,所以咱們要讓服務器端一直運行,最簡單的方法就是把 accept 方法放在一個循環內。
對上述代碼稍做改動:
1 import socket 2 import sys 3 4 HOST = '' # Symbolic name meaning all available interfaces 5 PORT = 8888 # Arbitrary non-privileged port 6 7 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 print 'Socket created' 9 10 try: 11 s.bind((HOST, PORT)) 12 except socket.error , msg: 13 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 14 sys.exit() 15 16 print 'Socket bind complete' 17 18 s.listen(10) 19 print 'Socket now listening' 20 21 #now keep talking with the client 22 while 1: 23 #wait to accept a connection - blocking call 24 conn, addr = s.accept() 25 print 'Connected with ' + addr[0] + ':' + str(addr[1]) 26 27 data = conn.recv(1024) 28 reply = 'OK...' + data 29 if not data: 30 break 31 32 conn.sendall(reply) 33 34 conn.close() 35 s.close()
很簡單只是加多一個 while 1 語句而已。
繼續運行服務器,而後打開另外三個命令行窗口。每一個窗口都使用 telnet 命令鏈接到服務器:
1 |
$ telnet localhost 5000 |
2 |
Trying 127.0.0.1... |
3 |
Connected to localhost. |
4 |
Escape character is '^]' . |
5 |
happy |
6 |
OK .. happy |
7 |
Connection closed by foreign host. |
服務器所在的終端窗口顯示的是:
1 |
$ python server.py |
2 |
Socket created |
3 |
Socket bind complete |
4 |
Socket now listening |
5 |
Connected with 127.0.0.1:60225 |
6 |
Connected with 127.0.0.1:60237 |
7 |
Connected with 127.0.0.1:60239 |
你看服務器不再退出了,好吧,用 Ctrl+C 關閉服務器,全部的 telnet 終端將會顯示 "Connection closed by foreign host."
已經很不錯了,可是這樣的通信效率過低了,服務器程序使用循環來接受鏈接併發送回應,這至關因而一次最多處理一個客戶端的請求,而咱們要求服務器可同時處理多個請求。
爲了處理多個鏈接,咱們須要一個獨立的處理代碼在主服務器接收到鏈接時運行。一種方法是使用線程,服務器接收到鏈接而後建立一個線程來處理鏈接收發數據,而後主服務器程序返回去接收新的鏈接。
下面是咱們使用線程來處理鏈接請求:
1 import socket 2 import sys 3 from thread import * 4 5 HOST = '' # Symbolic name meaning all available interfaces 6 PORT = 8888 # Arbitrary non-privileged port 7 8 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 print 'Socket created' 10 11 #Bind socket to local host and port 12 try: 13 s.bind((HOST, PORT)) 14 except socket.error , msg: 15 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 16 sys.exit() 17 18 print 'Socket bind complete' 19 20 #Start listening on socket 21 s.listen(10) 22 print 'Socket now listening' 23 24 #Function for handling connections. This will be used to create threads 25 def clientthread(conn): 26 #Sending message to connected client 27 conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string 28 29 #infinite loop so that function do not terminate and thread do not end. 30 while True: 31 32 #Receiving from client 33 data = conn.recv(1024) 34 reply = 'OK...' + data 35 if not data: 36 break 37 38 conn.sendall(reply) 39 40 #came out of loop 41 conn.close() 42 43 #now keep talking with the client 44 while 1: 45 #wait to accept a connection - blocking call 46 conn, addr = s.accept() 47 print 'Connected with ' + addr[0] + ':' + str(addr[1]) 48 49 #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function. 50 start_new_thread(clientthread ,(conn,)) 51 52 s.close()
運行上述服務端程序,而後像以前同樣打開三個終端窗口並執行 telent 命令:
01 |
$ telnet localhost 8888 |
02 |
Trying 127.0.0.1... |
03 |
Connected to localhost. |
04 |
Escape character is '^]' . |
05 |
Welcome to the server. Type something and hit enter |
06 |
hi |
07 |
OK...hi |
08 |
asd |
09 |
OK...asd |
10 |
cv |
11 |
OK...cv |
服務器端所在終端窗口輸出信息以下:
1 |
$ python server.py |
2 |
Socket created |
3 |
Socket bind complete |
4 |
Socket now listening |
5 |
Connected with 127.0 . 0.1 : 60730 |
6 |
Connected with 127.0 . 0.1 : 60731 |
線程接管了鏈接並返回相應數據給客戶端。
這即是咱們所要介紹的服務器端編程。
你可能會碰見一些問題:Bind failed. Error Code : 98 Message Address already in use,遇見這種問題只須要簡單更改服務器端口便可。