Python简单网络编程示例【客户端与服务端】
本文实例讲述了Python简单网络编程。分享给大家供大家参考,具体如下:
内容目录
1.客户端(client.py)
2.服务端(server.py)
一、客户端(client.py)
importsocket importsys port=70 host=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") forlineinfd.readlines(): sys.stdout.write(line)
程序通过socket.socket()建立一个Socket,参数告诉系统需要一个InternetSocket进行TCP通信。接着程序链接远程的主机名,并提供文件名。最后获得响应后在屏幕上打印出来。
测试
pythonclient.pyquux.org/
显示
iWelcometogopheratquux.org!fake(NULL)0 ifake(NULL)0 iThisserverhasalotofinformationofhistoricinterest,fake(NULL)0 ifunny,orjustplainentertaining--allpresentedinGopher.fake(NULL)0 iTherearemanymirrorshereofrareorvaluablefileswiththefake(NULL)0 iaimtopreservethemincasetheirhostdisappears.PLEASEREADfake(NULL)0 i"AboutThisServer"FORIMPORTANTNOTESANDLEGALINFORMATION.fake(NULL)0 ifake(NULL)0 0AboutThisServer/AboutThisServer.txtgopher.quux.org70+ 1Archives/Archivesgopher.quux.org70+ 1Books/Booksgopher.quux.org70+ 1Communication/Communicationgopher.quux.org70+ iThisdirectorycontainstheentiretextofthebookfake(NULL)0 i"WetheMedia:GrassrootsJournalismbythePeople,forthePeople"fake(NULL)0 ibyDanGillmorinvariousformats.fake(NULL)0 ifake(NULL)0 iFeelfreetodownloadandenjoy.fake(NULL)0 1Computers/Computersgopher.quux.org70+ 1CurrentIssuesandEvents(UpdatedApr.23,2002)/Currentgopher.quux.org70+ 1DevelopmentProjects/develgopher.quux.org70+ 0Gopher's10thAnniversary/3.0.0.txtgopher.quux.org70 1Government,Politics,Law,andConflict/Governmentgopher.quux.org70+ 0HowToHelp/HowToHelp.txtgopher.quux.org70+ 1HumorandFun/HumorandFungopher.quux.org70+ 1IndextoQuux.Org/Archives/indexgopher.quux.org70 1Internet/Internetgopher.quux.org70+ 1OtherGopherServers/Software/Gopher/serversgopher.quux.org70 1People/Peoplegopher.quux.org70+ 1Reference/Referencegopher.quux.org70+ 1SoftwareandDownloads/Softwaregopher.quux.org70+ 1TheGopherProject/Software/Gophergopher.quux.org70 0What'sNew/whatsnew.txtgopher.quux.org70+
二、服务端(server.py)
#coding:utf-8 importsocket host='' port=51421 s=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"Serverisrunningonport%d;pressCtrl-Ctoterminate."%port while1: clientsock,clientaddr=s.accept() clientfile=clientsock.makefile('rw',0) clientfile.write("Welcome,"+str(clientaddr)+"\n") clientfile.write("Pleaseenterastring:") line=clientfile.readline().strip() clientfile.write("Youentered%dcharacters.\n"%len(line)) clientfile.close() clientsock.close()
建立一个socket,设置成可复用的(reusable),绑定端口号51421(可选大于1024的任一值),调用listen()函数,开始等待来自客户端的请求,同时设定最多只有一个等候处理的链接。
主循环对a.accept()函数调用开始,程序连接一个客户端后立马停止,接收用户的输入。
运行一个例子
首先运行server.py
pythonserver.py
另开一个终端,连接localhost的51421端口。
jihite@ubuntu:~/web$telnetlocalhost51421 Trying127.0.0.1... Connectedtolocalhost. Escapecharacteris'^]'. Welcome,('127.0.0.1',59853) Pleaseenterastring:mm Youentered2characters. Connectionclosedbyforeignhost.
更多关于Python相关内容感兴趣的读者可查看本站专题:《PythonSocket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。