From 9b1c956dd75e0bcfe344aeaaca224d8b4b9ed6d6 Mon Sep 17 00:00:00 2001 From: Andreas Schlack Date: Thu, 3 May 2018 15:41:23 +0200 Subject: [PATCH 1/2] add proxy support --- pynma/pynma.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pynma/pynma.py b/pynma/pynma.py index 2fc5556..896e11f 100755 --- a/pynma/pynma.py +++ b/pynma/pynma.py @@ -1,4 +1,6 @@ #!/usr/bin/python +import os +import ssl from xml.dom.minidom import parseString @@ -115,7 +117,33 @@ def callapi(self, method, path, args): headers = { 'User-Agent': USER_AGENT } if method == "POST": headers['Content-type'] = "application/x-www-form-urlencoded" - http_handler = HTTPSConnection(API_SERVER) + proxy = None + if (proxy == None): + proxy = os.environ.get('https_proxy') + if (proxy == None): + proxy = os.environ.get('HTTPS_PROXY') + if (proxy == None): + # No proxy environement variable was defined, + # so continue without a proxy: + http_handler = HTTPSConnection(API_SERVER) + else: + # A proxy server was defined, it could be any of these formats: + # host:port + # http://host:port + # https://host:port + # https://host:port/ + # split the variable: + if proxy.startswith('https://'): + proxy=proxy[8:] + if proxy.startswith('http://'): + proxy=proxy[7:] + while proxy.endswith('/'): + proxy=proxy[:-1] + pparts = proxy.split(':') + ssl_context = ssl.create_default_context() + http_handler = HTTPSConnection(pparts[0], pparts[1], context=ssl_context) + http_handler.set_tunnel(API_SERVER, 443) + http_handler.request(method, path, urlencode(args), headers) resp = http_handler.getresponse() From b9c15a38340240f853f01f4314a30b2b889b496c Mon Sep 17 00:00:00 2001 From: Andreas Schlack Date: Thu, 3 May 2018 15:41:56 +0200 Subject: [PATCH 2/2] make test-message have two lines --- test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.py b/test.py index 61f1438..7186e1f 100755 --- a/test.py +++ b/test.py @@ -16,7 +16,7 @@ def main(keys): p.developerkey(dkey) p.addkey(keys) - res = p.push("test app", 'test event', 'test msg', 'http://example.com', batch_mode=False) + res = p.push("test app", 'test event', 'test msg\nwith a new-line', 'http://example.com', batch_mode=False) pprint(res) if __name__ == "__main__":