Get ipv4 address of visitors in http.server.BaseHTTPRequestHandler hosted

603 views Asked by At

When hosting a simple http.server.BaseHTTPRequestHandler server from Python, I would like to get the ip address of anyone accessing my server. I looked around, and I found solutions in Java and C, but nothing in Python. When I tried to convert the solutions in either language, they would not work. The GetIp from Java was not a part of the BaseHTTPRequestHandler class, nor was the UserHostName from C. I also am not serving from Flask, bottle, or any other serving platform, so I cannot use any of their methods. How would I do this?

1

There are 1 answers

0
User 12692182 On

There is subclass, request that is part of the BaseHTTPRequestHandler class, and inside it is are functions that will tell you ip addresses of your server, and the machines accessing it. Add this to your do_GET function: self.request.getpeername()

This will return a tuple of the form:

(their ipv4 address, their port)