9

में urllib2 के साथ काम करने के लिए नहीं लगता है मैं urllib2 का उपयोग कर मूल प्रमाणीकरण के साथ संरक्षित एक पृष्ठ डाउनलोड करने का प्रयास कर रहा हूं। मैं अजगर 2.7 का उपयोग कर रहा हूं लेकिन मैंने इसे अन्य कंप्यूटर पर अजगर 2.5 के साथ भी कोशिश की और सटीक उसी व्यवहार का सामना किया।HTTP मूल प्रमाणीकरण python

import urllib2 

passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
passman.add_password(None, "http://authenticationsite.com/', "protected", "password") 
authhandler = urllib2.HTTPBasicAuthHandler(passman) 
opener = urllib2.build_opener(authhandler) 

f = opener.open("http://authenticationsite.com/content.html") 
print f.read() 
f.close() 

दुर्भाग्य से सर्वर है मेरा तो मैं जानकारी साझा नहीं कर सकते नहीं,: मैं उदाहरण के रूप में बारीकी से मैं कर सकता और यहाँ के रूप में this guide में दिए गए पीछा कोड है कि मैं का उत्पादन किया है मैंने उन्हें ऊपर और नीचे बाहर swapped। जब मैं इसे चलाने मैं निम्नलिखित ट्रैसबैक मिल: अब

File 
"/usr/lib/python2.7/urllib2.py", line 
397, in open 
response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", 
line 510, in http_response 
'http', request, response, code, msg, hdrs) File 
"/usr/lib/python2.7/urllib2.py", line 
435, in error 
return self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", 
line 369, in _call_chain 
result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 
518, in http_error_default 
raise HTTPError(req.get_full_url(), code, 
msg, hdrs, fp) urllib2.HTTPError: HTTP 
Error 401: Authorization Required 

, दिलचस्प बात यह है कि जब मैं ngrep का उपयोग कर कंप्यूटर पर टीसीपी यातायात की निगरानी:

ngrep host 74.125.224.49 interface: 
wlan0 (192.168.1.0/255.255.255.0) 
filter: (ip) and (host 74.125.224.49 
) 
#### T 192.168.1.74:34366 -74.125.224.49:80 [AP] GET /content.html 
HTTP/1.1..Accept-Encoding: 
identity..Host: 
authenticationsite.com..Connection: 
close..User-Agent: 
Python-urllib/2.7.... 

## T 74.125.224.49:80 -192.168.1.74:34366 [AP] HTTP/1.1 401 Authorization Required..Date: Sun, 27 
Feb 2011 03:39:31 GMT..Server: 
Apache/2.2.3 (Red 
Hat)..WWW-Authenticate: Digest 
realm="protected", 
nonce="6NSgTzudBAA=ac585d1f7ae0632c4b90324aff5e39e0f1fc25 
05", algorithm=MD5, 
qop="auth"..Content-Length: 
486..Connection: close..Content-Type: text/html; 
charset=iso-8859-1....<!DOCTYPE HTML 
PUBLIC "-//IETF//DTD HTML 
2.0//EN">.<html><head>.<title>401 Authorization 
Required</title>.</head><body>.<h1>Authorization 
Required</h1>.<p>This server could not 
verify that you.are authorized to 
access the document.requested. Either 
you supplied the wrong.credentials 
(e.g., badpassword), or 
your.browser doesn't understand how to 
supply.the credentials 
required.</p>.<hr>.<address>Apache/2.2.3 
(Red Hat) Server at 
authenticationsite.com Port 
80</address>.</body></html>. 

#### 

ऐसा लगता है कि urllib2 कि फेंकने है प्रारंभिक 401 त्रुटि प्राप्त करने के बाद प्रमाण-पत्रों को आपूर्ति करने का प्रयास किए बिना अपवाद।

ngrep host 74.125.224.49 interface: 
wlan0 (192.168.1.0/255.255.255.0) 
filter: (ip) and (host 74.125.224.49 
) 
#### T 192.168.1.74:36102 -74.125.224.49:80 [AP] GET /content.html HTTP/1.1..Host: 
authenticationsite.com..User-Agent: 
Mozilla/5.0 (X11; U; Linux i686; 
en-US; rv:1.9.2.12) Gecko/20101027 
Firefox/3.6.12..Accept: text 
/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8..Accept-Language: 
en-us,en;q=0.5..Accept-Encoding: 
gzip,deflate..Accept-Charset: 
ISO-8859-1,utf-8;q=0.7,*;q=0.7..Keep-Alive: 
115..Connection: keep- alive.... 
## T 74.125.224.49:80 -192.168.1.74:36102 [AP] HTTP/1.1 401 Authorization Required..Date: Sun, 27 
Feb 2011 03:43:42 GMT..Server: 
Apache/2.2.3 (Red 
Hat)..WWW-Authenticate: Digest 
realm="protected", 
nonce="rKCfXjudBAA=0c1111321169e30f689520321dbcce37a1876b 
be", algorithm=MD5, 
qop="auth"..Content-Length: 
486..Connection: close..Content-Type: text/html; 
charset=iso-8859-1....<!DOCTYPE HTML 
PUBLIC "-//IETF//DTD HTML 
2.0//EN">.<html><head>.<title>401 Authorization 
Required</title>.</head><body>.<h1>Authorization 
Required</h1>.<p>This server could not 
verify that you.are authorized to 
access the document.requested. Either 
you supplied the wrong.credentials 
(e.g., badpassword), or 
your.browser doesn't understand how to 
supply.the credentials 
required.</p>.<hr>.<address>Apache/2.2.3 
(Red Hat) Server at 
authenticationsite.com Port 
80</address>.</body></html>. 

######### T 192.168.1.74:36103 -74.125.224.49:80 [AP] GET /content.html HTTP/1.1..Host: 
authenticationsite.com..User-Agent: 
Mozilla/5.0 (X11; U; Linux i686; 
en-US; rv:1.9.2.12) Gecko/20101027 
Firefox/3.6.12..Accept: text 
/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8..Accept-Language: 
en-us,en;q=0.5..Accept-Encoding: 
gzip,deflate..Accept-Charset: 
ISO-8859-1,utf-8;q=0.7,*;q=0.7..Keep-Alive: 
115..Connection: keep- alive..Authorization: Digest 
username="protected", 
realm="protected", 
nonce="rKCfXjudBAA=0c1111199162342689520550dbcce37a1876bbe", 
uri="/content.html", algorithm= MD5, 
response="3b65dadaa00e1d6a1892ffff49f9f325", 
qop=auth, nc=00000001, 
cnonce="7636125b7fde3d1b".... 

## 

और फिर साइट की सामग्री के साथ पीछा किया:

comparion के लिए, यहाँ ngrep के उत्पादन में जब मैं बजाय एक वेब ब्राउज़र में प्रमाणीकरण करते हैं।

मैं थोड़ी देर के लिए इसके साथ खेल रहा हूं और यह समझने में सक्षम नहीं हूं कि मैं क्या गलत कर रहा हूं। अगर कोई मेरी मदद कर सकता है तो मैं बहुत आभारी रहूंगा!

उत्तर

9

मुझे लगता है कि इस के कारण होती है:

WWW-Authenticate: Digest 

ऐसा प्रतीत होता है संसाधन डाइजेस्ट के बजाय मूल के साथ प्रमाणीकृत कर रहा है। जिसका अर्थ है कि आपको इसके बजाय urllib2.HTTPDigestAuthHandler का उपयोग करना चाहिए।

कोड हो सकता है

import urllib2 

passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
passman.add_password(None, "http://authenticationsite.com/", "protected", "password") 

# use HTTPDigestAuthHandler instead here 
authhandler = urllib2.HTTPDigestAuthHandler(passman) 
opener = urllib2.build_opener(authhandler) 

res = opener.open("http://authenticationsite.com/content.html") 
print res.read() 
res.close() 
+0

धन्यवाद, आप बिल्कुल सही थे! तुम्हारी मदद के लिए शुक्रिया! – foob

+0

मुझे अपनी पायथन लिपि से किसी साइट के यूआरएल को स्क्रैप करने में कोई समस्या है, जो पीडीएफ युक्त सभी साइटों को निकाल देगा। मैं एक प्रॉक्सी के पीछे काम कर रहा हूं जो उपयोगकर्ता नाम और पीडब्ल्यूडी मांगता है जब मैं पहली बार ब्राउजर खोलता हूं। मैं साइट को देखने और ब्राउज़र का उपयोग कर साइट से पीडीएफ डाउनलोड करने में सक्षम हूं। हालांकि, मैं पाइथन में कोड के माध्यम से ऐसा करने में सक्षम नहीं हूं। मुझे जिस त्रुटि का सामना करना पड़ रहा है वह है: "urllib.error.HTTPError: HTTP त्रुटि 401: प्रमाणीकरण आवश्यक" मुझे त्रुटि मिल रही है: "सारडिजिस्टएथहैंडलर निम्नलिखित योजना का समर्थन नहीं करता है: 'बातचीत करें' 'क्या मुझे कुछ याद आ रहा है? – Bonson

-1
import urllib2 
# Create an OpenerDirector with support for Basic HTTP Authentication... 
auth_handler = urllib2.HTTPBasicAuthHandler() 
auth_handler.add_password(realm='PDQ Application', 
          uri='https://mahler:8092/site-updates.py', 
          user='klem', 
          passwd='kadidd!ehopper') 
opener = urllib2.build_opener(auth_handler) 
# ...and install it globally so it can be used with urlopen. 
urllib2.install_opener(opener) 
urllib2.urlopen('http://www.example.com/login.html') 

- http://docs.python.org/library/urllib2.html#examples

+0

यह अनिवार्य रूप से मैं पहले से ही कर रहा था। विक्टर लिन ने दूसरे जवाब में बताया कि समस्या यह थी कि सर्वर वास्तव में मूल प्रमाणीकरण के बजाय डाइजेस्ट प्रमाणीकरण का उपयोग करता है। – foob

0

आप इस के लिए अजगर NTLM मॉड्यूल का उपयोग करने के लिए है: ntlm आयात HTTPNtlmAuthHandler

आयात urllib2

उपयोगकर्ता = "Your_username"

से

पासवर्ड = "your_Passwrd"

Passman = urllib2.HTTPPasswordMgrWithDefaultRealm()

passman.add_password (कोई नहीं "http://your_Home_location/", उपयोगकर्ता, पासवर्ड)

auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler (Passman)

सलामी बल्लेबाज = urllib2.build_opener (auth_NTLM)

urllib2।install_opener (सलामी बल्लेबाज)

url = "http://Your_home_location/sub_locations"

प्रतिक्रिया = urllib2.urlopen (यूआरएल)

हेडर = response.info()

प्रिंट ("हेडर: {}"। प्रारूप (हेडर))

शरीर = response.read()

प्रिंट ("प्रतिक्रिया:" + शरीर)

संबंधित मुद्दे