python2.x Web Starter Program
#!/usr/bin/python#-*-coding:utf-8-*-#valid only in python2.xImportOs#The OS module in the Python standard library contains common operating system featuresImportRe#Introducing Regular Expression ObjectsImportUrllib#used to encode URLs fromBasehttpserverImportHttpserver, Basehttprequesthandler#importing HTTP processing-related modules#custom handlers for handling HTTP requestsclassTesthttphandler (basehttprequesthandler):#Handling GET Requests defDo_get (self):#page Output Template stringTemplatestr=" "" "#compiling a regular expression into a pattern objectPattern = Re.compile (r'/qr\?s= ([^\&]+) \&QR=SHOW\+QR')#Match text with pattern, get match result, cannot match when will return noneMatch =Pattern.match (self.path) qrimg="' ifmatch:#use match to get grouped informationQrimg =''+ match.group (1) +'"/><br/>'+ Urllib.unquote (Match.group (1)) Self.protocal_version='http/1.1' #Set Protocol versionSelf.send_response (200)#Setting the response status codeSelf.send_header ("Welcome","contect")#Setting the response headerself.end_headers () self.wfile.write (Templatestr% qrimg)#Output Response Content#Start the service functiondefStart_server (port): Http_server= Httpserver (("', int (port)), Testhttphandler) Http_server.serve_forever ()#set up listening and receiving requestsOs.chdir ('Static')#change the working directory to the static directoryStart_server (8000)#Start the service, listen on port 8000
Python Web starter Program