如何获取向python SimpleHTTPServer发出请求的客户端的IP地址? [英] How to get IP address of the client who made request to python SimpleHTTPServer?

查看:163
本文介绍了如何获取向python SimpleHTTPServer发出请求的客户端的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python -m SimpleHTTPServer 设置 python 本地服务器,我使用 ngrock 公开该服务器并有一些公共 IP 地址,如 http://2ee94---.ngrok.io.现在我正在向公共 IP 地址发出 request.我想从请求中获取 IP 地址.但是我在终端中获得了唯一的状态.如何获取请求的详细信息(客户端的 IP 地址).

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  

推荐答案

我在设置自己的处理程序类后完成了,

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()

这篇关于如何获取向python SimpleHTTPServer发出请求的客户端的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆