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?
Asked
Active
Viewed 524 times
1

ChrisGPT was on strike
- 127,765
- 105
- 273
- 257

User 12692182
- 927
- 5
- 16
-
[Please don't add "solved" to your title or question](https://meta.stackexchange.com/a/116105/248627). Instead, [mark an answer correct by clicking on the checkmark](https://meta.stackexchange.com/q/5234/248627). You can also [add your own answer](https://stackoverflow.com/help/self-answer) and accept it if none of the ones you received solved your problem. – ChrisGPT was on strike Apr 27 '20 at 23:34
-
And please don't artificially "tag" your titles. Use the real tags that Stack Overflow provides. – ChrisGPT was on strike Apr 27 '20 at 23:35
-
In this: https://stackoverflow.com/help/how-to-ask, it said to be as descriptive as possible in your titles, so I tried to let the users know they were getting into a question about Python, because this is a topic that could be problematic across multiple languages. But yes, I will admit that I was wrong with the [solved] in the answer. – User 12692182 Apr 27 '20 at 23:50
-
Again, that's what _actual tags_ are for. You correctly included the [tag:python] tag; don't make up some tagging format to cram into your title. – ChrisGPT was on strike Apr 27 '20 at 23:52
1 Answers
2
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)

User 12692182
- 927
- 5
- 16