Python creates Web Server and pythonweb

Source: Internet
Author: User

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
    • GET, POST, HEAD... ...
  • Process requests
    • Save files
    • Execute CGI script
2. Create a simple Web Service
  • Use SimpleHTTPServer class
  • Implement the do_GET () method

  • It can be used as a server penetration code to penetrate the client.

  • Client code that can be used for penetration testing

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.
  

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.