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