一元网络论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 115|回复: 0

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

[复制链接]

3万

主题

3万

帖子

9万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
95418
发表于 2024-10-1 06:12:00 | 显示全部楼层 |阅读模式

"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:
          
             [Unit]
             Description=Python HTTP Server
             [Service]
             ExecStart=/usr/bin/python3 -m http.server 8090
             WorkingDirectory=/root/pic
             User=root
             Restart=always
             [Install]
             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.
    "
  • 回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|一元网络论坛

    GMT+8, 2024-11-25 01:49 , Processed in 0.094314 second(s), 20 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表