简体   繁体   English

在托管的 http.server.BaseHTTPRequestHandler 中获取访问者的 ipv4 地址

[英]Get ipv4 address of visitors in http.server.BaseHTTPRequestHandler hosted

When hosting a simple http.server.BaseHTTPRequestHandler server from Python, I would like to get the ip address of anyone accessing my server.从 Python 托管一个简单的http.server.BaseHTTPRequestHandler服务器时,我想获取访问我服务器的任何人的 ip 地址。 I looked around, and I found solutions in Java and C, but nothing in Python.我环顾四周,在 Java 和 C 中找到了解决方案,但在 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.来自GetIp的 GetIp 不是BaseHTTPRequestHandler class 的一部分,来自 C 的UserHostName也不是。 I also am not serving from Flask , bottle , or any other serving platform, so I cannot use any of their methods.我也不是从Flaskbottle或任何其他服务平台服务的,所以我不能使用他们的任何方法。 How would I do this?我该怎么做?

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.有一个子类, requestBaseHTTPRequestHandler class 的一部分,其中有一些函数会告诉你服务器的 ip 地址以及访问它的机器。 Add this to your do_GET function: self.request.getpeername()将此添加到您的do_GET function: self.request.getpeername()

This will return a tuple of the form:这将返回以下形式的元组:

(their ipv4 address, their port)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM