How to get client IP from SimpleHTTPServer

前端 未结 1 912
醉酒成梦
醉酒成梦 2021-01-20 06:07

Building a simple file server using the SimpleHTTPServer module in Python, however I\'m running into issues when trying to get the IP from a connecting client.

相关标签:
1条回答
  • 2021-01-20 06:20

    Indeed, the Handler class object is unrelated to specific instances. Set up your own handler class, like this:

    import SimpleHTTPServer
    import SocketServer
    
    
    class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
        def handle_one_request(self):
            print(self.client_address[0])
            return SimpleHTTPServer.SimpleHTTPRequestHandler.handle_one_request(self)
    
    print("Serving local directory")
    httpd = SocketServer.TCPServer(("", 8080), MyHandler)
    
    while True:
        httpd.handle_request()
    
    0 讨论(0)
提交回复
热议问题