標籤:
用戶端(client.py)
import socketimport sysport = 70host = sys.argv[1]filename = sys.argv[2]s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((host, port))fd = s.makefile("rw", 0)fd.write(filename + "\n")for line in fd.readlines(): sys.stdout.write(line)
程式通過socket.socket()建立一個Socket,參數告訴系統需要一個Internet Socket進行TCP通訊。接著程式連結遠端主機名稱,並提供檔案名稱。最後獲得響應後在螢幕上列印出來。
測試
python client.py quux.org /
顯示
iWelcome to gopher at quux.org!fake(NULL)0ifake(NULL)0iThis server has a lot of information of historic interest,fake(NULL)0ifunny, or just plain entertaining -- all presented in Gopher.fake(NULL)0iThere are many mirrors here of rare or valuable files with thefake(NULL)0iaim to preserve them in case their host disappears. PLEASE READfake(NULL)0i"About This Server" FOR IMPORTANT NOTES AND LEGAL INFORMATION.fake(NULL)0ifake(NULL)00About This Server/About This Server.txtgopher.quux.org70+1Archives/Archivesgopher.quux.org70+1Books/Booksgopher.quux.org70+1Communication/Communicationgopher.quux.org70+iThis directory contains the entire text of the bookfake(NULL)0i"We the Media: Grassroots Journalism by the People, for the People"fake(NULL)0iby Dan Gillmor in various formats.fake(NULL)0ifake(NULL)0iFeel free to download and enjoy.fake(NULL)01Computers/Computersgopher.quux.org70+1Current Issues and Events (Updated Apr. 23, 2002)/Currentgopher.quux.org70+1Development Projects/develgopher.quux.org70+0Gopher‘s 10th Anniversary/3.0.0.txtgopher.quux.org701Government, Politics, Law, and Conflict/Governmentgopher.quux.org70+0How To Help/How To Help.txtgopher.quux.org70+1Humor and Fun/Humor and Fungopher.quux.org70+1Index to Quux.Org/Archives/indexgopher.quux.org701Internet/Internetgopher.quux.org70+1Other Gopher Servers/Software/Gopher/serversgopher.quux.org701People/Peoplegopher.quux.org70+1Reference/Referencegopher.quux.org70+1Software and Downloads/Softwaregopher.quux.org70+1The Gopher Project/Software/Gophergopher.quux.org700What‘s New/whatsnew.txtgopher.quux.org70+
服務端(server.py)
# coding: utf-8import sockethost = ‘‘port = 51421s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind((host, port))s.listen(1) #每次最多隻有一個等候處理print "Server is running on port %d; press Ctrl-C to terminate." %portwhile 1: clientsock, clientaddr = s.accept() clientfile = clientsock.makefile(‘rw‘, 0) clientfile.write("Welcome, " + str(clientaddr) + "\n") clientfile.write("Please enter a string: ") line = clientfile.readline().strip() clientfile.write("You entered %d characters. \n" %len(line)) clientfile.close() clientsock.close()
建立一個socket,設定成可複用的(reusable),綁定連接埠號碼51421(可選大於1024的任一值),調用listen()函數,開始等待來自用戶端的請求,同時設定最多隻有一個等候處理的連結。
主迴圈對a.accept()函數調用開始,程式串連一個用戶端後立馬停止,接收使用者的輸入。
運行一個例子
首先運行server.py
python server.py
另開一個終端,串連localhost的51421連接埠。
[email protected]:~/web$ telnet localhost 51421Trying 127.0.0.1...Connected to localhost.Escape character is ‘^]‘.Welcome, (‘127.0.0.1‘, 59853)Please enter a string: mmYou entered 2 characters. Connection closed by foreign host.
python網路編程——簡單例子