Add headers support

This commit is contained in:
Rémi Oudin 2018-02-25 23:56:51 +01:00
parent 8ad46c0481
commit 71d9e18eec

View file

@ -21,7 +21,7 @@ class TorInstance():
TOR_RUNNER = 0 TOR_RUNNER = 0
@classmethod @classmethod
async def create(cls, history): async def create(cls, history, headers):
""" Factory creation of tor processes""" """ Factory creation of tor processes"""
socks_port = cls.BASE_SOCKS_PORT + cls.TOR_RUNNER socks_port = cls.BASE_SOCKS_PORT + cls.TOR_RUNNER
control_port = cls.BASE_CONTROL_PORT + cls.TOR_RUNNER control_port = cls.BASE_CONTROL_PORT + cls.TOR_RUNNER
@ -32,6 +32,7 @@ class TorInstance():
self.control_port = control_port self.control_port = control_port
self.data_dir = data_dir self.data_dir = data_dir
self.history = history self.history = history
self.headers = headers
self.proxy = "socks5://127.0.0.1:{}".format(self.socks_port) self.proxy = "socks5://127.0.0.1:{}".format(self.socks_port)
self.create_session() self.create_session()
self.process = tor.launch_tor_with_config( self.process = tor.launch_tor_with_config(
@ -49,6 +50,7 @@ class TorInstance():
self.data_dir = "" self.data_dir = ""
self.history = None self.history = None
self.proxy = "" self.proxy = ""
self.headers = {}
self.session = None self.session = None
self.process = None self.process = None
@ -72,6 +74,7 @@ class TorInstance():
conn = ProxyConnector(remote_resolve=True) conn = ProxyConnector(remote_resolve=True)
self.session = aiohttp.ClientSession( self.session = aiohttp.ClientSession(
connector=conn, connector=conn,
headers=self.headers,
request_class=ProxyClientRequest request_class=ProxyClientRequest
) )
@ -105,7 +108,7 @@ class TorInstance():
async def main(): async def main():
""" Test function """ """ Test function """
for _ in range(3): for _ in range(3):
instance = await TorInstance.create(None) instance = await TorInstance.create(None, {"user-agent" : "Blah"})
await instance.query("https://python.org/") await instance.query("https://python.org/")
print("One page received") print("One page received")
await instance.kill() await instance.kill()