#703 pyhaystack through a proxy script

DANIEL VILLA Thu 16 May 2019

Is there any way to connect to a haystack server that uses a proxy server script using pyhaystack? I have managed to do so on my own using a pypac session but I would rather leverage the broader functionality of pyhaystack if possible.

Stuart Longland Fri 17 May 2019

pyhaystack internally uses the python-requests module for its HTTP access.

https://2.python-requests.org/en/master/user/advanced/#proxies

That page suggests the library should respect the "industry standard" HTTP_PROXY and HTTPS_PROXY environment variables, so one way that may work is to do the following:

from sys import environ

environ['HTTP_PROXY'] = 'http://your.proxy.server:3128'
environ['HTTPS_PROXY'] = 'http://your.proxy.server:3128'

prior to establishing your session with pyhaystack.

Alternatively for more flexibility, all session objects take a keyword argument; http_args. You can pass a dict object to this parameter with any options for the HTTP client class:

https://pyhaystack.readthedocs.io/en/latest/pyhaystack.client.http.html#pyhaystack.client.http.base.HTTPClient

The value you pass here is passed direct to python-requests, so:

from pyhaystack.client import get_instance

session = get_instance(implementation='yourserver', arg1='val1', … etc,
                       http_args=dict(
                          proxies={
                              'http': 'http://your.proxy.server:3128',
                              'https': 'http://your.proxy.server:3128',
                              'http://example.org/haystack': 'http://special.proxy.server:3128',
                          }
                       ))

It is also possible to use SOCKS proxies, if that's what you require.

Login or Signup to reply.