0

I have a python script that will be run as an exe (packaged using py2exe) on a machine without Python installed. I currently have my proxy information written into the script using a proxy handler with urllib2 (see code below), but I would like to pass the user and pw as parameters written via command line, which I'm using to run the exe. Anyone have any idea how to do this? Any help is appreciated!

def get_response(url, query='', get_json=True):
    opener = urllib2.build_opener(
                urllib2.HTTPHandler(),
                urllib2.HTTPSHandler(),
                urllib2.ProxyHandler( 
                   {'https': 'http://user:password@myIPaddress:8080',
                    'http': 'http://user:password@myIPaddress:8080'}
                ))
    urllib2.install_opener(opener)
    encoded = urllib.urlencode(query)
    request = urllib2.Request(url, encoded)
    if get_json:
        return json.loads(urllib2.urlopen(request).read())
    return urllib2.urlopen(request).read()
Crazy Otto
  • 125
  • 2
  • 13
  • `sys.argv` doesn't work? – martineau Jan 26 '15 at 13:13
  • haven't tried -- how/where would I use that? – Crazy Otto Jan 26 '15 at 14:36
  • 1
    See the [`sys`](https://docs.python.org/2/library/sys.html#module-sys) module docs. Normally `argv[0]` is the script name and `argv[1:]` are the rest of the command line arguments. These might be offset by 1 with py2exe -- you'll have to check it out. – martineau Jan 26 '15 at 15:08
  • if i'm not mistaken and assuming an offset for py2exe, the proxy handler should look something like this? `{'https': 'http://' + sys.arg[0] + ':' + sys.arg[0] + 'IPaddress:8080', 'https://' + sys.arg[0] + ':' + sys.arg[0] + 'IPaddress:8080'} – Crazy Otto Jan 26 '15 at 16:06
  • Can you print, display, or otherwise output the value of `sys.argv` (note it's `argv` not `arg`) from within your py2exe'd script and see what's in it? That would tell you whether it has the information in it and what the indices of the user name and password are so you can set up the proxy with them. – martineau Jan 26 '15 at 16:20
  • this worked: {'https': 'http://' + sys.argv[0] + ':' + sys.argv[1] + '@192.168.104.103:8080', 'http': 'http://' + sys.argv[0] + ':' + sys.argv[1] + '@192.168.104.103:8080'} – Crazy Otto Jan 27 '15 at 08:58
  • Good, I guess, because from some things I've read, such as [this](http://stackoverflow.com/questions/9497370/making-exe-file-from-python-that-uses-command-line-arguments) answer, the `argv` argument indices don't get offset like I thought they might with py2exe -- so the ones you say worked shouldn't have. – martineau Jan 27 '15 at 14:04
  • See also [Py2exe Environment](http://www.py2exe.org/index.cgi/Py2exeEnvironment) where it says "The first item in `sys.argv` is the full pathname of the executable, the rest are the command line arguments." – martineau Jan 27 '15 at 15:06
  • yup, you're right...but now i'm getting an error with indexes of 1 (for user) and 2 (for password), here's the traceback: Traceback (most recent call last): File "BackupService_Reworked_26_arg_unc.py", line 221, in # ----- Change service url as necessary ----- # File "BackupService_Reworked_26_arg_unc.py", line 62, in login print response['error'] File "BackupService_Reworked_26_arg_unc.py", line 56, in get_response – Crazy Otto Jan 28 '15 at 11:13
  • traceback cont'd: File "urllib2.pyc", line 126, in urlopen File "urllib2.pyc", line 391, in open File "urllib2.pyc", line 409, in _open File "urllib2.pyc", line 369, in _call_chain File "urllib2.pyc", line 1169, in https_open File "urllib2.pyc", line 1136, in do_open urllib2.URLError: – Crazy Otto Jan 28 '15 at 11:13
  • so in sum, it works when the user and pw are hardcoded in the script as part of the proxy url strings, but not when like this: {'https': 'http://' + sys.argv[1] + ':' + sys.argv[2] + '@192.168.104.103:8080', 'http': 'http://' + sys.argv[1] + ':' + sys.argv[2] + '@192.168.104.103:8080'} -- odd. – Crazy Otto Jan 28 '15 at 12:10
  • ...and it works with indices of 0 (for user) and 1 (for password), right? I was just pointing out that doesn't seem to match the documentation and other things I've read. Did you ever just display what was in `argv` to see what's in it like I suggested? What version of Python and py2exe are you using? – martineau Jan 28 '15 at 14:20
  • no, it doesn't work with 0 and 1 either, that was my mistake. i just tried printing my password -- the issue is a special character in my proxy password (an ampersand) that i have to escape when typing it into the command line as a parameter. wrapping the password in double quotes does the trick with 1 and 2 as the user and pw indices, respectively. – Crazy Otto Jan 29 '15 at 08:34

0 Answers0