• Python 搭建简单的Web Server


    In this post I’m going to explain how to create a simple Python CGI Server. To illustrate the concepts involved more clearly I will not attempt to make this server extensible, rather I will try to keep the code as simple and clear as possible.

    To start with we need to create a directory for our server and CGI scripts to reside in. I simply created a directory named ‘server’ in my home directory, but you may name it anything you wish and place it anywhere in your filesystem.

    The next step is to create a simple CGI server. To do this open up your favorite text editor and write the program below. (I’ll go through it line-by-line at the end of this tutorial.)

    #!/usr/bin/env python
     
    import BaseHTTPServer
    import CGIHTTPServer
    import cgitb; cgitb.enable()  ## This line enables CGI error reporting
     
    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler
    server_address = ("", 8000)
    handler.cgi_directories = ["/"]
     
    httpd = server(server_address, handler)
    httpd.serve_forever()
    Save this file in the ‘server’ directory as ‘server.py’, or whatever name tickles your fancy, and give it executable permissions.
    That’s it for the server! Simple wasn’t that? Now that we have a server ready to go we need something for it to serve. For that we will create a simple “Hello World!” CGI script. Open up your text editor again and type in the following program.
    #!/usr/bin/env python
     
    print "Content-type: text/html"
    print
    print "<title>Test CGI</title>"
    print "<p>Hello World!</p>"

    Save this file in the ‘server’ directory as ‘test_cgi.py’ and give it executable permissions, just like the server.py file.

    Now we have all the components in place. To start your Python CGI server simply open up a terminal and cd into your ‘server’ directory. When you are there simply type the following command.

    ./server.py
     

    Your server is now fully operational! To see your first page fire up a browser and type the following into the location bar.

    http://localhost:8000/test_cgi.py

    You should be greeted by a webpage that proudly proclaims “Hello World!”. If you are not, make sure to check your file permissions (both the server and CGI script should be given executable permissions) and make sure you copied the programs above verbatim. To kill your server simply go back to the terminal and hit ‘ctrl-c’.

    Server Code Line-By-Line

    For those of you who want a more in-depth analysis of what this code is doing, here is a line-by-line explanation.

    The first thing we need to do is make this script executable on the command line. The following ‘she-bang’ line simplifies this process so that we can type: ./server.py instead of the more verbose: python server.py

    #!/usr/bin/env python
    The next two lines import the necessary libraries. The first, BaseHTTPServer, provides us with a simple web server and CGIHTTPServer provides us with a request handler.
    import BaseHTTPServer
    import CGIHTTPServer

    The following line enables in-browser CGI error reporting. This can be very handy as you develop CGI scripts, however if you don’t want to expose the internals of your program to users you may wish to log the output of this to a file instead.

    import cgitb; cgitb.enable()  ## This line enables CGI error reporting

    The next two lines create a server and a request handler.

    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler

    The next line specifies an address for the server, in this case we want to use ‘localhost’ so simply specifing “” will give us this. This line also specifies a port number to associate with our server. You can choose whatever port number you wish.

    server_address = ("", 8000)

    The next line specifies where the CGI scripts will reside in relation to the ‘server’ directory. In this example we put our CGI script in the base directory by using “”, however you may perfer to place your scripts in a special ‘cgi’ or ‘cgi-bin’ directory.

    handler.cgi_directories = ["/"]

    The next line actually ‘creates’ our server, passing it the server address and port number, as well as the CGI handler. If you’re wondering why we name it httpd it stands for HTTP Daemon.

    httpd = server(server_address, handler)

    The final line puts our program into an infinite loop so that the server can ‘serve_forever’. Until you kill the server with ‘ctrl-c’ from the terminal, it will continue to listen for requests and serve the appropriate web pages.

    httpd.serve_forever()

    http://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/

  • 相关阅读:
    客户端回传事件接口IPostBackEventHandler
    《敏捷无敌》—— 一本非常好看的“IT技术言情小说”
    面向对象之设计
    Zac谈网络编辑需要注意的SEO技巧
    面向对象之领悟
    《网络营销实战密码》推荐
    设计模式建造者模式(builder)
    设计模式工厂方法(FactoryMethod)
    struts1.x与struts2的比较表
    设计模式原型模式(ProtoType)
  • 原文地址:https://www.cnblogs.com/xjsllll/p/2999717.html
Copyright © 2020-2023  润新知