標籤:
網路通訊的基本介面是socket,它擴充了作業系統的基本I/O到網路網路通訊。socket可以通過socket()函數來建立,通過connect()函數來串連。得到了socket,可以確定本地和遠程端點的IP地址和連接埠號碼。socket對不同的協議來說都是一種通用的介面,它可以處理TCP和UDP通訊。
建立socket需要兩步:建立socket對象、串連到遠程伺服器上
import socketprint "Creating socket"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #指明通訊類型和協議家族print "Done!"print "Connecting to remote host..."s.connect(("www.baidu.com", 80))print "Done"
運行結果
Creating socketDone!Connecting to remote host...Done
尋找連接埠號碼
通過協議名和連接埠名尋找連接埠號碼(unix系統可以在etc/services中找到對應的連接埠號碼)
import socketprint "Creating socket"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "Done!"print "Looking up port number..."port = socket.getservbyname("http", "tcp")print portprint "done!"print "Connecting to remote host..."s.connect(("www.baidu.com", port))print "Done!!"
運行結果
Creating socketDone!Looking up port number...80done!Connecting to remote host...Done!!
從socket獲得資訊
import socketprint "Creating socket"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "Done!"print "Looking up port number..."port = socket.getservbyname("http", "tcp")print portprint "done!"print "Connecting to remote host..."s.connect(("www.baidu.com", port))print "Done!!"print "Connected from", s.getsockname() #獲得本地機器的ip和連接埠號碼(連接埠號碼由作業系統隨機分配,每次都不一樣)print "Connected to", s.getpeername() #獲得串連機器的ip和連接埠號碼
一次運行結果
Creating socketDone!Looking up port number...80done!Connecting to remote host...Done!!Connected from (‘192.168.209.129‘, 56245)Connected to (‘220.181.112.244‘, 80)
利用socket通訊
socket對象:send()、sendto()、recv()、recvfrom()
檔案類對象:read()、write()、readline()
處理錯誤
import socket, syshost = sys.argv[1]textport = sys.argv[2]filename = sys.argv[3]try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)except socket.error, e: print "Strange error creating socket: %s" % e sys.exit(1)try: port = int(textport)except ValueError: try: port = socket.getservbyname(textport, ‘tcp‘) except socket.error, e: print "Couldn‘t find your port: %s" % e sys.exit(1)try: s.connect((host, port))except socket.gaierror, e: print "Address-related error connecting to server: %s" %e sys.exit(1)except socket.error, e: print "Connection error: %s" %e sys.exit(1)try: s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename)except socket.error, e: print "Error sending data: %s" %e sys.exit(1)print "---------------------------------------"while 1: try: buf = s.recv(2048) except socket.error, e: print "========Error receving data: %s=======" %e sys.exit(1) if not len(buf): break sys.stdout.write(buf)print "---------------------------------------"
改進
#coding: utf-8import socketimport sysimport timehost = sys.argv[1]textport = sys.argv[2]filename = sys.argv[3]try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)except socket.error, e: print "Strange error creating socket: %s" % e sys.exit(1)try: port = int(textport)except ValueError: print "ValueError" try: port = socket.getservbyname(textport, ‘tcp‘) except socket.error, e: print "Couldn‘t find your port: %s" % e sys.exit(1)try: s.connect((host, port))except socket.gaierror, e: #主機名稱不對 print "Address-related error connecting to server: %s" %e sys.exit(1)except socket.error, e: #串連遠程主機有問題 print "Connection error: %s" %e sys.exit(1)print "Sleepping........."time.sleep(10)print "Continuing."try: s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename)except socket.error, e: print "Error sending data: %s" % e sys.exit(1)try: s.shutdown(1)except socket.error, e: print "Error sending data (detected by shutdown):%s" % e sys.exit(1) print "---------------------------------------"while 1: try: buf = s.recv(2048) print "try" except socket.error, e: print "========Error receving data: %s=======" %e sys.exit(1) if not len(buf): #接受的為空白 print "breaK" break sys.stdout.write(buf)print "---------------------------------------"
python 網路編程——用戶端