python网络编程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

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.3Con guringApacheonLinuxforusingwithCGIscripts.........256Commonprotocols266.1DesigningTelnetapplications............................266.2FileTransferProtocol................................286.3SMTPprotocol....................................297TODOs30ListofFigures1TCPconnection...................................92UDPconnection...................................10LISTOFFIGURES33Threadedserverdiagram...............................144Forkserverdiagram.................................175DesigningaTCPconnectionwithstatepattern..................2041INTRODUCTION1IntroductionNetworkprogrammingisabuzzwordnowinthesoftworld.Weseethemarket lledwithanavalancheofnetworkorientedapplicationslikedatabaseservers,games,Javaservletsandapplets,CGIscripts,di erentclientsforanyimaginableprotocolandtheexamplesmaycon-tinue.Today,morethenhalfoftheapplicationsthathitthemarketarenetworkoriented.Datacommunicationbetweentwomachines(onlocalnetorInternet)isnotanymoreacu-riositybutisadaytodayreality.\ThenetworkisthecomputersaystheSunMicrosystem'smottoandtheyareright.Thecomputerisnomoreseenasaseparateentity,dialoggingonlywithit'shumanoperatorbutaspartofalargersystem-thenetwork,boundviadatalinkswithotherthousandsofothermachines.Thispaperispresentingapossiblewayofdesigningnetwork-orientedapplicationsusingPython.BecausetheauthorisaLinuxfan,theexamplescontainedinthispaperarerelatedtoLinux1andapologizesalltheWindowsorMacOSusers(fans?)foranyinconvenienceonreadingthistext.Withalittlee ort,theexamplesareportabletoanothernon-UNIXoperationsystem.Presentingaquickstructureofthispaper, rstfoursectionsaredealingwithprimitivedesign{atsocketlevel{ofnetworkapplications.Theremainingsectionsaretreatingspeci cprotocolslikehttp,ftp,telnetorsmtp.ThesectiondealingwithhttpwillcontainasubsectionaboutwritingCGIscriptsandusingthecgimodule.Goingfurtheronmoreconcretesubjects,wearegoingtoanalyzethepossibilitiesofnetworkprogrammingprovidedinPython.RawnetworksupportisimplementedinPythonthroughthesocketmodule,thismodulecomprisingmostlyofthesystem-calls,functionsandconstantsde nedbythe4.3BSDInterprocessCommunicationfacilities(see[1]),implementedinobject-orientedstyle.Pythono ersasimpleinterface(muchsimplerthanthecorrespondingCimplementation,thoughbasedonthisone)toproperlycreateanduseasocket.Primarily,isde nedthesocket()functionreturningasocketobject2.Thesockethasseveralmethods,correspondingtotheirpairsfromCsys/socket.h,likebind(),connect(),listen()oraccept().ProgrammersaccustomedwithsocketusageunderClanguage3will ndveryeasytotranslatetheirknowledgeinthemore-easy-to-usesocketimplementationunderPython.Pythoneliminatesthedauntingtaskof llingstructureslikesockaddrinorhostentandeasetheuseofpreviouslymentionedmethodsorfunctions{parameterpassingandfunctionscallareeasiertohandle.Somenetwork-orientedfunctionsareprovidedtoo:gethostbyname(),getprotobyname()orconversionfunctionsntohl(),htons(),usefulwhenconvertingintegerstoandfromnetworkformat.ThemoduleprovidesconstantslikeSOMAXCONN,INADDR*,usedingesockopt()orsetsockopt()functions.ForacompletelistofabovementionedconstantscheckyourUNIXdocumentationonsocketimplementation.Pythonprovidebesidesocket,additionalmodules(infactthereisawholebundleofthem)supportingthemostcommonnetworkprotocolsatuserlevel.Forexamplewemay ndusefulmoduleslikehttplib,ftplib,telnetlib,smtplib.ThereisimplementedsupportforCGIscriptingthroughcgimodule,amoduleforURLparsing,classesdescrib

1 / 31
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功