0

The script below works fine when I am using script at home (same PC!):

import urllib.request

x = urllib.request.urlopen('https://www.google.com/')
print(x.read())

the same does not work using the same script when I am connected at work. I do not know proxy address or IP, so my script should use the same way as IE or anything else on this PC.

I found some suggestions about using proxy , but the point it I do not know proxy IP or details. When I move the script to another PC it might have different proxy, so I think hardcoding it is not good approach. Can I somehow inform Python to autodetect proxy settings?

rdr
  • 49
  • 1
  • 10
  • `does not work` phrase is very vague and open to interpretation. Giving error details might unblock you, rather than going to a solution related to `autodetect proxy settings`? – vishwarajanand Nov 14 '17 at 08:12
  • ok. fair point. When I execute it from company network I get this: urllib.error.URLError: . When I excute the same from home - everything works fine, so the question is how to properly inform Python to autodetect local proxy settings.The page which I try to open is no blocked (I am sure about it). – rdr Nov 14 '17 at 12:05

1 Answers1

0
  1. Going by your eample, I am assuming you are doing a https call over proxy. The urllib documentation hints its not supported. So, instead you may have to settle down with http.

  2. In order to validate that there is nothing wrong with your setup, you may try to do open the IP directly:


    import urllib
    # IP address for `http://www.google.com` is `216.58.205.196`
    x = urllib.urlopen('http://216.58.205.196')
    print x.read()

  1. A. There are lots of complaints about Python's trippy auto-detect proxy settings in various other threads. I had this issue only once years ago and I opted for setting a fixed proxy instead of trying to configure auto-detect. To know your proxy, you can go to chrome url chrome://net-internals/#proxy or run netstat -an | grep EST command.

    B. Once you have proxy address, you can use following code:


    import urllib
    # IP address for `http://www.google.com` is `216.58.205.196`
    x = urllib.urlopen('http://216.58.205.196', 
                        proxies={'http': 'http://www.someproxy.com:3128'})
    print x.read()

  1. If you cannot avoid https, then you may consider requests library. I didn't test this, but requests documentation looks quite promising. This is how it can be done!

    import requests
    proxies = {
      'http': 'http://10.10.1.10:3128',
      'https': 'http://10.10.1.10:1080',
    }
    requests.get('https://example.org', proxies=proxies)


Edit:

1: You may need to setup proxy authentication in order for 3.B. to work

2: For Special characters, you would need to have the password in unicode: 'p@ssw0rd'.decode('utf-8')

Hope this helps!

vishwarajanand
  • 1,021
  • 13
  • 23
  • in the meantime I manged to move one step forward with this script and get error: raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 407: Proxy Authentication Required. I think I know whats next step. thanks for advice – rdr Nov 15 '17 at 12:01
  • You would be using some `authentication` for your proxy, and because this script does not provide any, it would obviously fail. Added it in the answer, thanks for pointing out! – vishwarajanand Nov 16 '17 at 05:59
  • thanks. What shall I do in the case my password contains '@' as a special character? This is real case. My real password has this special character. if I need to use something like this: http://username:pass@word@url:port, then how Python knows which '@' is end of the password? Anyway. I want to user Python to grab some data from one of cloud solutions we use. Data are in JSON, but all runs with https. I also found information that HTTPS is not supported, so what is other option? – rdr Nov 16 '17 at 18:50
  • For special characters in password, you would need to convert into unicode. – vishwarajanand Nov 17 '17 at 03:21
  • I think, you may go with `requests` library then. Added in the answer though not tested it! – vishwarajanand Nov 17 '17 at 04:23
  • that's what I did and now all works fine. thanks a lot for help. At my company I also needed to provide logon details to go via PROXY, but now I get what I want. – rdr Nov 17 '17 at 18:00