python网络编程之读取网站根目录实例
本文实例讲述了python网络编程之读取网站根目录的方法,分享给大家供大家参考。
具体实现方法如下:
importsocket,sys port=70 host="quux.org" filename="//" s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((host,port)) s.sendall(filename+"\r\n") while(1): buf=s.recv(2048) ifnotbuf: break sys.stdout.write(buf)
本文实例运行环境为Python2.7.6
该实例会返回quux.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+
希望本文所述对大家的Python程序设计有所帮助