尝试在没有多线程的情况下执行简单代码时可以正常工作:
q=Queue.Queue()
def run_job(input,url):
try:
passman=urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager
passman.add_password(None, request_url, username, password)
# because we have put None at the start it will always
# use this username/password combination for urls
# for which URL is a super-url
authhandler = urllib2.HTTPBasicAuthHandler(passman)
# create the AuthHandler
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
# All calls to urllib2.urlopen will now use our handler
# Make sure not to include the protocol in with the URL, or
# HTTPPasswordMgrWithDefaultRealm will be very confused.
# You must (of course) use it when fetching the page though.
print >>sys.stderr, "Sending request: "+request_url
# authentication is now handled automatically for us`enter code here`
request=urllib2.Request(request_url, urllib.urlencode(post_parameters))
pagehandle = urllib2.urlopen(request)
data=pagehandle.read().decode('utf8')
q.put(data.encode('utf8')
if __name__ == "__main__":
infile=(codecs.getreader('utf-8'))(sys.stdin)
while(1):
start1 = datetime.datetime.now()
t1=threading.Thread(target=run_job,args(doc,'http://example.com'))
t1.start()
start2 = datetime.datetime.now()
t2=threading.Thread(target=run_job,args=(doc,'http//example.com'))
t2.start()
start3 = datetime.datetime.now()
t3=threading.Thread(target=run_job,args=(doc,'http//example.com'))
t1.join()
end1 = datetime.datetime.now()
t2.join()
end2 = datetime.datetime.now()
t3.join()
end3=datetime.datetime.now()
但在使用 multithreading throws the error
:
HTTPError: HTTP Error 401: Authorization Required for the line
pagehandle = urllib2.urlopen(request)
example.com是一个假网址,原始链接是我们自己的机器链接,我无法透露。 我不明白为什么?