在python中用BaseHTTPRequestHandler模块儿时获取客户端的ip和端口
2015-06-18 23:26
1101 查看
1、在python安装路径下找到E:\Program Files\Python25\Lib\SocketServer.py
2,由于python原装模块并没有为我们提供获取客户端IP地址和port的接口(至少目前我没有找到),因此需要自己动手丰衣足食了。修改SocketServer中代码,在其中添加一个全局变量,以获取客户端的addr。在handle_request()函数当中添加如下代码
代码如下:
application_client_addr=('192.168.0.1',9999)#全局变量
def handle_request(self):
"""Handle one request, possibly blocking."""
try:
request, client_address = self.get_request()
global application_client_addr#声明全局变量
application_client_addr=client_address#获取客户端addr
except socket.error:
return
if self.verify_request(request, client_address):
try:
self.process_request(request, client_address)
except:
self.handle_error(request, client_address)
self.close_request(request)
3.如何调用呢?在你的程序当中
import SocketServer
print SocketServer.application_client_addr
取完收工!
2,由于python原装模块并没有为我们提供获取客户端IP地址和port的接口(至少目前我没有找到),因此需要自己动手丰衣足食了。修改SocketServer中代码,在其中添加一个全局变量,以获取客户端的addr。在handle_request()函数当中添加如下代码
代码如下:
application_client_addr=('192.168.0.1',9999)#全局变量
def handle_request(self):
"""Handle one request, possibly blocking."""
try:
request, client_address = self.get_request()
global application_client_addr#声明全局变量
application_client_addr=client_address#获取客户端addr
except socket.error:
return
if self.verify_request(request, client_address):
try:
self.process_request(request, client_address)
except:
self.handle_error(request, client_address)
self.close_request(request)
3.如何调用呢?在你的程序当中
import SocketServer
print SocketServer.application_client_addr
取完收工!
相关文章推荐
- URL,URI的区别
- TCP预先派生子进程服务程序,accept无上锁保护
- 网络请求(XUtils)+JSON解析(Gson)
- 检查HTTP 的 Digest 认证代码示例-JSP
- 【枚举】bzoj3391 [Usaco2004 Dec]Tree Cutting网络破坏
- 【Linux 内核网络协议栈源码剖析】af_inet.c——INET Socket层(2)
- http简介
- Android快速入门(转自 农民伯伯: http://www.cnblogs.com/over140/)
- iOS开发网络数据之AFNetworking使用1
- 黑马程序员------网络编程
- muduo网络图书馆评测
- 黑马程序员----Java网络编程
- Linux网络编程——tcp并发服务器(epoll实现)
- java https请求 源码 微信
- poj 1459 Power Network 网络流
- Java进行https请求需要证书的问题 unable to find valid certification path to requested target
- 网络传输中send与recv需要注意的地方----------来自博客讨论
- linux中手工编译安装httpd服务全操作过程
- Java servlet example - HttpServletRequest, getPathInfo, and getRequestURI
- 第十六章:网络IPC:套接字