다음 코드는 연결이 표준 라이브러리에서 HTTPLIB에 의해 urllib3 열이 아닌 것을, 사실을 ncat -v -l 127.0.0.1 8000
문제이다 사용하여 자식 내가 그것을 테스트 (특히 requests.packages.urllib3.poolmanager.PoolManager._new_pool()
)
을 요청 필요 .
import socket
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3 import PoolManager, HTTPConnectionPool
try:
from http.client import HTTPConnection
except ImportError:
from httplib import HTTPConnection
class MyAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize):
self.poolmanager = MyPoolManager(num_pools=connections,
maxsize=maxsize)
class MyPoolManager(PoolManager):
def _new_pool(self, scheme, host, port):
# Important!
if scheme == 'http' and host == my_host and port == my_port:
return MyHTTPConnectionPool(host, port, **self.connection_pool_kw)
return super(PoolManager, self)._new_pool(self, scheme, host, port)
class MyHTTPConnectionPool(HTTPConnectionPool):
def _new_conn(self):
self.num_connections += 1
return MyHTTPConnection(host=self.host,
port=self.port,
strict=self.strict)
class MyHTTPConnection(HTTPConnection):
def connect(self):
"""Connect to the host and port specified in __init__."""
# Original
# self.sock = socket.create_connection((self.host, self.port),
# self.timeout, self.source_address)
# Important!
self.sock = my_socket
if self._tunnel_host:
self._tunnel()
if __name__ == '__main__':
import time
my_host = '127.0.0.1'
my_port = 8000
my_socket = socket.create_connection((my_host, my_port))
time.sleep(4)
s = requests.Session()
s.mount('http://', MyAdapter())
s.get('http://127.0.0.1:8000/foo')
편집 : connectionpool의
또는 직접 monkeypatching :
class MyHTTPConnection(HTTPConnection):
def connect(self):
self.sock = my_socket
if self._tunnel_host:
self._tunnel()
requests.packages.urllib3.connectionpool.HTTPConnection = MyHTTPConnection
if __name__ == '__main__':
my_host = '127.0.0.1'
my_port = 8000
my_socket = socket.create_connection((my_host, my_port))
requests.get('http://127.0.0.1:8000/foo')
이 monkeypatching 나를 위해 작동하지 않습니다. 지난 4 년 동안 요청이 변경되었거나 수업이 변경되지 않았다면 잘 모르겠다. (적어도 __init__와 connect 메소드는 호출되지 않는다.) 파이썬 3.5 및 요청 2.18.1 사용하고 있습니다. – Tom