admin 发表于 2024-10-1 06:12:00

Python3搭建HTTP服务器作为图片分享网站


"To set up an HTTP server as a picture upload service in Python 3, you need to follow these steps:
1. Install Python 3 and pip:
   
   [*]Use the command `sudo apt update` to update the package list.
   [*]Then use `sudo apt install python3` to install Python 3.
   [*]Finally, use `sudo apt install python3-pip` to install pip for Python 3.
   
   
2. Check if the installation is successful:
   
   [*]Run the commands `python3 --version` and `pip3 --version` to check the version of both Python 3 and pip.
   

3. Set up the HTTP server:
   
   [*]Create a file called `python-http.py` in your home directory using `touch /home//python-http.py` or any other method to create a new file.
   [*]In this file, add the following code:
      
         import http.server
         from http.server import SimpleHTTPRequestHandler
         PORT = 8090
         Handler = SimpleHTTPRequestHandler
         
         class MyHandler(Handler):
         def end_headers(self):
             self.send_header('X-Powered-By', 'Python/{0}'.format(http.server.__version__))
             super().end_headers()
         
         def log_message(self, format, *args):
             return
      
         with http.server.HTTPServer(('localhost', PORT), MyHandler) as httpd:
         print("serving at port", PORT)
         httpd.serve_forever()
      
       This creates a simple HTTP server that listens on localhost port 8090.
   
   

4. Configure Systemd:
   
   [*]Create a systemd unit file named `python-http.service` in the `/etc/systemd/system/` directory.
   [*]The contents of the file should be like this:
      
         
         Description=Python HTTP Server
         
         ExecStart=/usr/bin/python3 -m http.server 8090
         WorkingDirectory=/root/pic
         User=root
         Restart=always
         
         WantedBy=multi-user.target
      
       This sets up the HTTP server with the specified configuration.
   
   

5. Reload systemd, start the service, and enable it:
   
   [*]Reload systemd by running `sudo systemctl daemon-reload`.
   [*]Start the service by running `sudo systemctl start python-http`.
   [*]Enable the service by running `sudo systemctl enable python-http`.
   
   

6. Check the status of the service:
   
   [*]Check the status of the service by running `sudo systemctl status python-http`.
   

7. Use reverse proxy services like Nginx, Caddy, or Cloudflare Tunnels to redirect traffic to the HTTP server:
   
   [*]Upload the necessary files using WinSCP or any other FTP client.
   [*]Set up the reverse proxy server (e.g., Nginx) with the IP address and port of the HTTP server.
   [*]Forward the requests to the HTTP server through the reverse proxy.
   

8. Test the setup by accessing the server's IP address and port number.
"
页: [1]
查看完整版本: Python3搭建HTTP服务器作为图片分享网站