Search code examples
pythonserversimplehttpserver

How to get IP address of the client who made request to python SimpleHTTPServer?


I am setting python local server using python -m SimpleHTTPServer, I made that server publicly available using ngrock and have some public IP address like http://2ee94---.ngrok.io. Now I am making the request to the public IP address. I want to get IP address from request. But I am getting the only status in the terminal. How to get details (IP address of client) of the request.

HTTP Requests                                                                   
-------------                                                                   

GET  /                         200 OK                                           
GET  /                         200 OK  

Solution

  • I got it done after setting up my own handler class,

    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)
    
    httpd = SocketServer.TCPServer(("", 8080), MyHandler)
    
    while True:
        httpd.handle_request()