从 python urllib2.urlopen 调用不一致地收到 401 错误
我有一个 python 脚本,它使用 urllib2 进行一系列 url 调用。 url 在 http 上,但需要身份验证。我目前正在尝试运行该脚本,以便它将进行 100 多次调用。每次运行脚本时,有些调用会失败并显示错误代码 401,而有些则通过。所有调用均使用相同的用户名和密码针对同一 URL。 (每次我运行脚本时,失败的调用都不一样,有时第一次调用失败,有时有效。)
有什么想法为什么 401 可能会不一致地发生吗?
打印到屏幕上的错误消息是...
这是负责进行 url 调用的方法:
def simpleExecuteRequest(minX, minY, maxX, maxY, type) :
url = 'http://myhost.com/geowebcache/rest/seed/mylayer.xml'
msgTemplate = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<seedRequest>
<name>mylayer</name>
<bounds>
<coords>
<double>%s</double>
<double>%s</double>
<double>%s</double>
<double>%s</double>
</coords>
</bounds>
<gridSetId>nyc</gridSetId>
<zoomStart>0</zoomStart>
<zoomStop>10</zoomStop>
<format>image/png</format>
<type>%s</type>
<threadCount>1</threadCount>
</seedRequest>
"""
message = msgTemplate%(minX, minY, maxX, maxY, type)
headers = { 'User-Agent' : "Python script", 'Content-type' : 'text/xml; charset="UTF-8"', 'Content-length': '%d' % len(message) }
passwordManager = urllib2.HTTPPasswordMgrWithDefaultRealm()
passwordManager.add_password(None, url, 'username', 'xxx')
authenticationHandler = urllib2.HTTPBasicAuthHandler(passwordManager)
proxyHandler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxyHandler, authenticationHandler)
urllib2.install_opener(opener)
try :
request = urllib2.Request(url, message, headers)
response = urllib2.urlopen(request)
content = response.read()
print 'success'
except IOError, e:
print e
有时输出将如下所示...
<urlopen error (10053, 'Software caused connection abort')>
success
success
<urlopen error (10053, 'Software caused connection abort')>
<urlopen error (10053, 'Software caused connection abort')>
...
当运行 1 分钟后,它可能看起来像这样...
success
<urlopen error (10053, 'Software caused connection abort')>
success
success
<urlopen error (10053, 'Software caused connection abort')>
在两个运行相同以相同的顺序提供了最小/最大 x/y 和类型的一系列输入。 ...
I have a python script that makes a series of url calls using urllib2. The url is on http, but requires authentication. I am currently trying to run the script such that it will make over 100 calls. Every time I run the script, some calls fail with error code 401, and some pass. All calls are for the same URL using the same username and password. (Each time I run the script it is not the same calls that fail, sometimes the first call fails, sometimes it works.)
Any ideas why a 401 might occur inconsistently?
The error message printed to the screen is...
Here is the method responsible for making the url call:
def simpleExecuteRequest(minX, minY, maxX, maxY, type) :
url = 'http://myhost.com/geowebcache/rest/seed/mylayer.xml'
msgTemplate = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<seedRequest>
<name>mylayer</name>
<bounds>
<coords>
<double>%s</double>
<double>%s</double>
<double>%s</double>
<double>%s</double>
</coords>
</bounds>
<gridSetId>nyc</gridSetId>
<zoomStart>0</zoomStart>
<zoomStop>10</zoomStop>
<format>image/png</format>
<type>%s</type>
<threadCount>1</threadCount>
</seedRequest>
"""
message = msgTemplate%(minX, minY, maxX, maxY, type)
headers = { 'User-Agent' : "Python script", 'Content-type' : 'text/xml; charset="UTF-8"', 'Content-length': '%d' % len(message) }
passwordManager = urllib2.HTTPPasswordMgrWithDefaultRealm()
passwordManager.add_password(None, url, 'username', 'xxx')
authenticationHandler = urllib2.HTTPBasicAuthHandler(passwordManager)
proxyHandler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxyHandler, authenticationHandler)
urllib2.install_opener(opener)
try :
request = urllib2.Request(url, message, headers)
response = urllib2.urlopen(request)
content = response.read()
print 'success'
except IOError, e:
print e
Sometimes the output will look like this...
<urlopen error (10053, 'Software caused connection abort')>
success
success
<urlopen error (10053, 'Software caused connection abort')>
<urlopen error (10053, 'Software caused connection abort')>
...
When run 1 minute later it might look like this...
success
<urlopen error (10053, 'Software caused connection abort')>
success
success
<urlopen error (10053, 'Software caused connection abort')>
On both runs the same series of inputs for min/max x/y and type were provided in the same order.
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码对我来说看起来是正确的,所以我没有看到问题。
以下是关于如何继续的一些想法:
curl
在命令行计算出 http 请求。requests 库比 urllib2 更容易使用
当您收到回复,打印出来标题,以便您可以看到发生了什么
而不是
除了IOError,e
使用<代码>除了 IOError 作为 e。新方法可以保护您免受难以发现的错误的影响。我假设您编辑了用户名和密码,并在自己的脚本中使用了真实的用户名和密码;-)
The code looks correct to me, so I don't see the issue.
Here are a few thoughts on how to proceed:
I usually work-out the http requests at the command line using
curl
before translating it into a script.The requests library is easier to use than urllib2
When you receive a response, print out the headers so you can see what is going on
Instead of
except IOError, e
useexcept IOError as e
. The new way protects you from hard to find errors.I presume you redacted the username and password and are using the real ones in your own script ;-)