Python Web starter Program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.