您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Python Network Programming
PythonNetworkProgrammingbySebastianV.TiponutTechnicalUniversityTimisoaraVersion0.00,16.July20012LISTOFFIGURESContents1Introduction42Basicsocketusage52.1Creatingasocket...................................52.2Connectingasocketanddatatransfer.......................62.3Bindinganametosocket..............................62.4Listeningandacceptingconnections........................62.5UDPsockets.....................................72.6Closingthesocket..................................72.7Usingfunctionsprovidedinsocketmodule.....................82.7.1Functionsbasedonresolverlibrary.....................82.7.2Service-relatedfunctions...........................82.7.3Miscellaneousfunctions...........................83Basicnetworkstructuresdesign93.1DesigningaTCPserver...............................93.2TheTCPclient....................................113.3Modelingdatagramapplications..........................114Advancedtopicsonservers134.1Buildingapristineenvironment...........................134.2Handlingmultipleconnections............................134.2.1Threadedservers...............................144.2.2Usingselect..................................154.2.3Forkservers..................................164.3Dealingwithclasses.................................184.3.1Simpleconnectionobject..........................184.3.2Applyingadesignpattern..........................204.4Advancedaspectsconcerningclients........................225HTTPprotocol235.1CGImodule......................................235.1.1BuildasimpleCGIscript..........................235.1.2UsingCGImodule..............................245.1.3ConguringApacheonLinuxforusingwithCGIscripts.........256Commonprotocols266.1DesigningTelnetapplications............................266.2FileTransferProtocol................................286.3SMTPprotocol....................................297TODOs30ListofFigures1TCPconnection...................................92UDPconnection...................................10LISTOFFIGURES33Threadedserverdiagram...............................144Forkserverdiagram.................................175DesigningaTCPconnectionwithstatepattern..................2041INTRODUCTION1IntroductionNetworkprogrammingisabuzzwordnowinthesoftworld.Weseethemarketlledwithanavalancheofnetworkorientedapplicationslikedatabaseservers,games,Javaservletsandapplets,CGIscripts,dierentclientsforanyimaginableprotocolandtheexamplesmaycon-tinue.Today,morethenhalfoftheapplicationsthathitthemarketarenetworkoriented.Datacommunicationbetweentwomachines(onlocalnetorInternet)isnotanymoreacu-riositybutisadaytodayreality.\ThenetworkisthecomputersaystheSunMicrosystem'smottoandtheyareright.Thecomputerisnomoreseenasaseparateentity,dialoggingonlywithit'shumanoperatorbutaspartofalargersystem-thenetwork,boundviadatalinkswithotherthousandsofothermachines.Thispaperispresentingapossiblewayofdesigningnetwork-orientedapplicationsusingPython.BecausetheauthorisaLinuxfan,theexamplescontainedinthispaperarerelatedtoLinux1andapologizesalltheWindowsorMacOSusers(fans?)foranyinconvenienceonreadingthistext.Withalittleeort,theexamplesareportabletoanothernon-UNIXoperationsystem.Presentingaquickstructureofthispaper,rstfoursectionsaredealingwithprimitivedesign{atsocketlevel{ofnetworkapplications.Theremainingsectionsaretreatingspecicprotocolslikehttp,ftp,telnetorsmtp.ThesectiondealingwithhttpwillcontainasubsectionaboutwritingCGIscriptsandusingthecgimodule.Goingfurtheronmoreconcretesubjects,wearegoingtoanalyzethepossibilitiesofnetworkprogrammingprovidedinPython.RawnetworksupportisimplementedinPythonthroughthesocketmodule,thismodulecomprisingmostlyofthesystem-calls,functionsandconstantsdenedbythe4.3BSDInterprocessCommunicationfacilities(see[1]),implementedinobject-orientedstyle.Pythonoersasimpleinterface(muchsimplerthanthecorrespondingCimplementation,thoughbasedonthisone)toproperlycreateanduseasocket.Primarily,isdenedthesocket()functionreturningasocketobject2.Thesockethasseveralmethods,correspondingtotheirpairsfromCsys/socket.h,likebind(),connect(),listen()oraccept().ProgrammersaccustomedwithsocketusageunderClanguage3willndveryeasytotranslatetheirknowledgeinthemore-easy-to-usesocketimplementationunderPython.Pythoneliminatesthedauntingtaskofllingstructureslikesockaddrinorhostentandeasetheuseofpreviouslymentionedmethodsorfunctions{parameterpassingandfunctionscallareeasiertohandle.Somenetwork-orientedfunctionsareprovidedtoo:gethostbyname(),getprotobyname()orconversionfunctionsntohl(),htons(),usefulwhenconvertingintegerstoandfromnetworkformat.ThemoduleprovidesconstantslikeSOMAXCONN,INADDR*,usedingesockopt()orsetsockopt()functions.ForacompletelistofabovementionedconstantscheckyourUNIXdocumentationonsocketimplementation.Pythonprovidebesidesocket,additionalmodules(infactthereisawholebundleofthem)supportingthemostcommonnetworkprotocolsatuserlevel.Forexamplewemayndusefulmoduleslikehttplib,ftplib,telnetlib,smtplib.ThereisimplementedsupportforCGIscriptingthroughcgimodule,amoduleforURLparsing,classesdescrib
本文标题:Python Network Programming
链接地址:https://www.777doc.com/doc-4074604 .html