事实证明,这是相当牵连的.
这是一个猴子补丁,同时使用请求版本1.2.3:
在HTTPConnectionPool上包装_make_request方法,以便在HTTPResponse实例上存储来自socket.getpeername()的响应.
对于我在python 2.7.3上,这个实例在response.raw._original_response上可用.
from requests.packages.urllib3.connectionpool import HTTPConnectionPool
def _make_request(self,conn,method,url,**kwargs):
response = self._old_make_request(conn,method,url,**kwargs)
sock = getattr(conn,'sock',False)
if sock:
setattr(response,'peer',sock.getpeername())
else:
setattr(response,'peer',None)
return response
HTTPConnectionPool._old_make_request = HTTPConnectionPool._make_request
HTTPConnectionPool._make_request = _make_request
import requests
r = requests.get('http://www.google.com')
print r.raw._original_response.peer
产量:
('2a00:1450:4009:809::1017', 80, 0, 0)
啊,如果有一个代理涉及或响应被分块,HTTPConnectionPool._make_request不被调用.
所以这里是一个新版本修补httplib.getresponse:
import httplib
def getresponse(self,*args,**kwargs):
response = self._old_getresponse(*args,**kwargs)
if self.sock:
response.peer = self.sock.getpeername()
else:
response.peer = None
return response
httplib.HTTPConnection._old_getresponse = httplib.HTTPConnection.getresponse
httplib.HTTPConnection.getresponse = getresponse
import requests
def check_peer(resp):
orig_resp = resp.raw._original_response
if hasattr(orig_resp,'peer'):
return getattr(orig_resp,'peer')
运行:
>>> r1 = requests.get('http://www.google.com')
>>> check_peer(r1)
('2a00:1450:4009:808::101f', 80, 0, 0)
>>> r2 = requests.get('https://www.google.com')
>>> check_peer(r2)
('2a00:1450:4009:808::101f', 443, 0, 0)
>>> r3 = requests.get('http://wheezyweb.readthedocs.org/en/latest/tutorial.html#what-you-ll-build')
>>> check_peer(r3)
('162.209.99.68', 80)
还检查运行与代理集;代理地址被返回.
更新2016/01/19
rsp = requests.get('http://google.com', stream=True)
# grab the IP while you can, before you consume the body!!!!!!!!
print rsp.raw._fp.fp._sock.getpeername()
# consume the body, which calls the read(), after that fileno is no longer available.
print rsp.content
更新2016/05/19
从这些评论中,复制这里以查看,Richard Kenneth Niescior提供以下确认使用请求2.10.0和Python 3.
rsp=requests.get(..., stream=True)
rsp.raw._connection.sock.getpeername()