2009-12-15 3 views
4

Python에서 https url을 어떻게 열지?기본 액세스 인증을 사용하여 Python에서 https url 읽기

import urllib2 

url = "https://user:[email protected]/path/ 
f = urllib2.urlopen(url) 
print f.read() 

준다 : 당신은 그런 urllib2.open에 자격 증명을 전달할 수 없습니다

httplib.InvalidURL: nonnumeric port: '[email protected]' 
+0

을 읽을 수있는 방법은 https를 실패한 적이없는 사실로 * 검색 * 이미 요청을받은 것 같은 질문을 게시하기 전에 유래. 검색 결과는 다음과 같습니다. http://stackoverflow.com/search?q=%5Bpython%5D+https –

답변

2

. 귀하의 경우 user은 도메인 이름으로 해석되고 [email protected]은 포트 번호로 해석됩니다.

11

이것은 파이썬에서 URL의가 나를

import urllib2, base64 
username = 'foo' 
password = 'bar' 
auth_encoded = base64.encodestring('%s:%s' % (username, password))[:-1] 

req = urllib2.Request('https://somewebsite.com') 
req.add_header('Authorization', 'Basic %s' % auth_encoded) 
try: 
    response = urllib2.urlopen(req) 
except urllib2.HTTPError, http_e: 
    # etc... 
    pass 
+0

'request.add_header ('인증', b'Basic '+ base64.b64encode (사용자 이름 + b': '+'비밀번호))' – jfs

관련 문제