2013-10-31 81 views
2
[email protected]:/$ python2.7 
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
[GCC 4.6.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import urllib 
>>> url1 = 'http://www.google.com' 
>>> url2 = 'https://www.google.com' 
>>> f = urllib.urlopen(url1) 
>>> f = urllib.urlopen(url2) 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "/usr/lib/python2.7/urllib.py", line 87, in urlopen 
    return opener.open(url) 
File "/usr/lib/python2.7/urllib.py", line 211, in open 
    return getattr(self, name)(url) 
File "/usr/lib/python2.7/urllib.py", line 355, in open_http 
    'got a bad status line', None) 
IOError: ('http protocol error', 0, 'got a bad status line', None) 
>>> 

當我嘗試連接到https站點時,使用urllib我得到了上面的錯誤。 代理正確設置。調試python代碼,我注意到urllib.py中沒有執行ssl庫上的導入。所以,https呼叫也不會執行。任何人都可以幫助我嗎?我必須使用urllib,而不是urllib2或另一個。提前致謝。使用python2.7上的urllib無法訪問https站點

+0

看看這裏的想法:http://stackoverflow.com/questions/3747037/urllib-py-doesnt-work-with-https –

回答

0

這不是什麼問題你至少寫它的方式:

$ python 
Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import urllib 
>>> url1 = 'http://www.google.com' 
>>> url2 = 'https://www.google.com' 
>>> f = urllib.urlopen(url1) 
>>> f = urllib.urlopen(url2) 
>>> f.read()[:15] 
'<!doctype html>' 
>>> 

所以,這就是事實並非如此。它必須是你的環境或你的配置的東西。你說你正在使用代理?

編輯:

我能夠通過一個開放的代理打開它(因爲誰知道它是粗略的將不包括所述代理 - 代替你自己的代理服務器:

$ python 
Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import urllib2 
>>> proxy_handler = urllib2.ProxyHandler({'http': 'http://some-sketchy-open-proxy'}) 
>>> opener = urllib2.build_opener(proxy_handler) 
>>> opener.open('https://www.google.com') 
<addinfourl at 140512985881056 whose fp = <socket._fileobject object at 0x7fcbba9b1ed0>> 
>>> _.read()[:15] 
'<!doctype html>' 
>>> 

與自己的代理URL嘗試這種方式(請注意,我用的urllib2,不urllib的)希望幫助

編輯2:!

只使用urllib的:

$ python 
Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3] on linux2 
Type "copyright", "credits" or "license()" for more information. 
>>> import urllib 
>>> proxies = {'http': '189.112.3.87:3128'} 
>>> url = 'https://www.google.com' 
>>> filehandle = urllib.urlopen(url,proxies=proxies) 
>>> filehandle.read()[:15] 
'<!doctype html>' 
>>> 
+0

事實上,我使用的是代理如http:// myserver:8080。 Curl,Wget和git工作正常,即使通過代理。是否還有其他需要執行的配置? Tks – camizao

+0

@camizao用一個使用代理的版本編輯答案。 – chucksmash

+0

我真的很感激,chucksmash,但我只需要使用urllib。 TKS – camizao

相關問題