2014-02-28 125 views
1

我找不到這方面的文檔。是否有可能在python 2.7中運行https請求?python中的HTTPS請求2.7

我試過這個代碼3.2,但這些模塊在2.7中不存在。

import urllib.request 
r = urllib.request.urlopen('https://openapi.etsy.com/v2/users/redluck2013/profile?  fields=transaction_sold_count&api_key=1pmmjgt3j4nz5ollhzz2hvib') 
print(r.read()) 

回答

1

2.7:

import urllib2 

r = urllib2.urlopen('https://openapi.etsy.com/v2/users/redluck2013/profile?fields=transaction_sold_count&api_key=1pmmjgt3j4nz5ollhzz2hvib') 

print(r.read()) 

http://docs.python.org/2/howto/urllib2.html

1

包裝urllib.request在Python 3加入,它不會在Python 2.7存在。

在Python 2.7版,使用urllib.urlopen

此無關以https。

0

對於HTTPS,請注意:

Warning HTTPS requests do not do any verification of the server’s certificate. 

http://docs.python.org/2/library/urllib2.html

如果您還需要HTTPS驗證,蟒蛇的要求是非常有用的圖書館

import requests 
url = https://openapi.etsy.com/v2/users/redluck2013/profile?fields=transaction_sold_count&api_key=1pmmjgt3j4nz5ollhzz2hvib 
r = requests.get(url, verify=True) 
content = r.text 

對於驗證,只要確保將True傳遞給verify參數即可。

這裏更多:

+0

的代碼是不是有效的Python。此外,您不需要明確的'verify = True',例如'requests.get('https://docs.python-requests.org')'失敗,因爲主機名不匹配。 – jfs