ChristianTremblay / pyhaystack

Pyhaystack is a module that allow python programs to connect to a haystack server project-haystack.org. Connection can be established with Niagara Platform running the nhaystack, Skyspark and Widesky. For this to work with Anaconda IPython Notebook in Windows, be sure to use "python setup.py install" using the Anaconda Command Prompt in Windows. If not, module will be installed for System path python but won't work in the environment of Anaconda IPython Notebook. You will need hszinc 1.3+ for this to work.
Apache License 2.0
74 stars 32 forks source link

pyhaystack through a proxy script #72

Closed ChristianTremblay closed 5 years ago

ChristianTremblay commented 5 years ago

Documenting this from project-haystack website @sjlongland

DANIEL VILLA jeu. 16 mai 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 ven. 17 mai 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.

ChristianTremblay commented 5 years ago

Will be available if needed.