1

I m trying to run the following simple code:

import urllib2
import base64

username = "some_user"
password = "some_pass"
url = "some_url"

req = urllib2.Request(url)
authheader =  "Basic %s" % base64.encodestring('%s:%s' % (username, password))
req.add_header("Authorization", authheader)
req.add_header('User-agent', 'Mozilla/5.0')

resp = urllib2.urlopen(req)
print resp.read()

It works fine on windows, but on the same machine under Linux it doesnt work, it gives URL exception with code 503. I m sure there is no problem with the server, because it works fine with Mozzila and curl (both under lin and win). What can cause this problem?

user1724641
  • 331
  • 1
  • 11
  • What exception does it throw? – Games Brainiac Jan 21 '14 at 10:23
  • It throws URLError with code 503 (Service Unavailable) - but I am sure the service is available cause I ve tried it with Mozzila and curl. (503 in docs: http://docs.python.org/release/2.6/howto/urllib2.html) – user1724641 Jan 21 '14 at 10:30
  • I suspect it has to do with the base64 and utf. Has your file a utf8 charset? Try adding this ontop: `#!/usr/bin/env python # -*- coding: utf-8 -*- ` – Jimmy Kane Jan 21 '14 at 12:51

1 Answers1

1

I was having a similar issue and eventually found that I had an environment variable "http_proxy" actually pointing at a proxy server. My problem went away when I either deleted the environment variable or explicitly set it to nothing in my Python script.