Python creates Web Server and pythonweb
1. Web service application Working Mechanism
- Listen to port 80 or port 443
- 80 is a common http protocol, and 443 is https.
- Wait for client request
- Process requests
- Save files
- Execute CGI script
2. Create a simple Web Service
Enter the python editing mode from the terminal and enter the following code:
>>> >>> import SocketServer>>> import SimpleHTTPServer>>> >>> >>> httpServer = SocketServer.TCPServer(("",8888),SimpleHTTPServer.SimpleHTTPRequestHandler)>>> >>> >>> httpServer.serve_forever()
Import the SocketServer and SimpleHTTPServer modules to create an instance of TCPServer,("",8888)The local IP address and port 8888 are bound. Server_forever () indicates continuous listening.
Open the browser, enter the local IP address and port number, and view the result:
The browser lists the files in the current directory, and then empty the server processing status.
The ip address on the left is inconsistent with the IP address of my browser because I use a proxy. I will not explain it here. As for the Code 404, let's take a look at the small sign on the left side of the browser address and click View, because the identity is not verified.
If you only use the web service to list files, you can. If you want to go further, provide different output services. For example, you want different users to access and output different results. As a security tester, if you want to view different parameters sent by the server, take a look at the Code:
#! /Usr/bin/env python # _ * _ coding = UTF-8 _ * _ import SocketServerimport SimpleHTTPServer # create an HTTPserver processing class that inherits SimpleHTTPServerclass HttpRequestHandler (SimpleHTTPServer. simpleHTTPRequestHandler): def do_GET (self): if self. path = '/root': self. wfile. write ('This page is only for root') self. wfile. write (self. headers) else: SimpleHTTPServer. simpleHTTPRequestHandler. do_GET (self) httpServer = SocketServer. TCPServer ("", 8888), HttpRequestHandler) httpServer. serve_forever ()
Write an HTTP processing class, inherit SimpleHTTPServer, and override the do_GET () method. Run this script and access it in a browser.
If you only enter the same address as above (10.128.175.36:8888).10.128.175.36:8888/rootAccess the root directory and check the result again:
From the result, the access is no longer available, and details of the http header have been dumped. Including the Host IP address and port, user-agent, and Accept.
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
This user-agent describes information about the google browser on the local machine. It is useful for web crawlers to forge http headers.