Python SimpleHTTPServer add POST Method

Source: Internet
Author: User

Easy to implement. Run the script on the terminal and open localhost: 8000 in the browser to view the file in the current directory.

You can also add more functional statements in the do_GET and do_POST methods to implement richer functions.


[Python]
Mport SimpleHTTPServer
Import SocketServer
Import cgi
 
PORT = 8000
 
Class ServerHandler (SimpleHTTPServer. SimpleHTTPRequestHandler ):
 
Def do_GET (self ):
SimpleHTTPServer. SimpleHTTPRequestHandler. do_GET (self)
 
Def do_POST (self ):
Form = cgi. FieldStorage ()
SimpleHTTPServer. SimpleHTTPRequestHandler. do_GET (self)
 
Handler = ServerHandler
 
Httpd = SocketServer. TCPServer ("", PORT), Handler)
 
Print "serving at port", PORT
Httpd. serve_forever ()

Import SimpleHTTPServer
Import SocketServer
Import cgi

PORT = 8000

Class ServerHandler (SimpleHTTPServer. SimpleHTTPRequestHandler ):

Def do_GET (self ):
SimpleHTTPServer. SimpleHTTPRequestHandler. do_GET (self)

Def do_POST (self ):
Form = cgi. FieldStorage ()
SimpleHTTPServer. SimpleHTTPRequestHandler. do_GET (self)

Handler = ServerHandler

Httpd = SocketServer. TCPServer ("", PORT), Handler)

Print "serving at port", PORT
Httpd. serve_forever ()


Use the do_GET Method


[Python]
Print "hi"

Print "hi"
Will output hi on the terminal (that is, the server side)

If you use


[Python]
Self. wfile. write ("hi ")

Self. wfile. write ("hi ")
You can output hi on the browser side.

You can also use HTML statements in self. wfile. write () to display webpages in your browser.

 


Ps: the result of self-study.
 

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.