0

I'm working on a company network right now, and I've come across a problem where my scripts cannot connect to external networks. I'm just wondering if anyone knows common practices in network security that may cause this?

Ex. I can visit www.example.com on firefox, but my python script will get a timeout error if it tries to connect.

These scripts work perfectly fine on another network or if I change the URL to something on the local network.

import urllib.request
f = urllib.request.urlopen('http://www.python.org/')
print(f.read(300))

ANSWER: the browser uses the network's proxy. Scripts also have to use that proxy to run

import urllib.request

proxy = urllib.request.ProxyHandler({'http': '127.0.0.1'})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
req = urllib.request.urlopen('http://www.google.com')
print(req.read())
Alter
  • 3,332
  • 4
  • 31
  • 56

1 Answers1

1

It is very likely that your browser is configured to use a proxy. If that is true, then you will need to augment your python script with ProxyHandler (see Proxy with urllib2)

Community
  • 1
  • 1
linuxfan
  • 1,110
  • 2
  • 17
  • 29
  • Well that was easy, but how does 127.0.0.1 get the proxy? I thought 127 addresses just return the request to the computer – Alter Oct 17 '14 at 22:40
  • @Alter The argument to ProxyHandler() should be a dictionary that contains the proxy settings used in your network. 127.0.0.1 cannot be a valid proxy. Ask your network admin, or look in your browser settings to view the proxy's IP address and port number. – linuxfan Oct 17 '14 at 22:42
  • I'm very confused, it works perfectly with 127.0.0.1 - could it be that python knows this is an invalid proxy and gets the system proxy instead? – Alter Oct 17 '14 at 22:49
  • I lied, it does not work perfectly, I was mistaking any response for the correct response... I was getting my localhost webpage – Alter Oct 17 '14 at 22:52
  • See https://docs.python.org/2/library/urllib2.html#urllib2.ProxyHandler If you don't pass any arguments to ProxyHandler(), it will attempt to detect your http_proxy and https_proxy environment variables. Please get details of your proxy from the admin or look in your browser settings. In firefox, go to Preferences->Advanced->Settings to view your proxy settings. – linuxfan Oct 17 '14 at 22:56