无法在Python中发出HTTP请求

时间:2019-07-22 14:12:04

标签: python http urllib2

我尝试使用以下代码在url上获取内容:

request  = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request,timeout=20)

我得到了错误:

urllib2.URLError: <urlopen error [Errno 10054] An existing connection was forcibly closed by the remote host>

但是我可以通过浏览器使用相同的凭据访问url

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 因为通过网络浏览器,我实际上使用的是代理,但未在Python脚本中使用它。 后来我在发出HTTP请求之前包括了代理,它可以工作。

相关问题