我有一堆任务要在 cPanel 中自动化。http://videos.cpanel.net/cpanel-api-automation/中描述了一个 cPanel API,但我尝试了我认为对我来说更容易的方法......
import urllib, urllib2, ssl
url = 'https://mysite.com:2083/login'
user_agent = 'Mozilla/5.0 meridia (Windows NT 5.1; U; en)'
values = {'name':cpaneluser,
'pass':cpanelpw}
headers = {'User-Agent':user_agent}
data = urllib.urlencode(values)
req = urllib2.Request(url,data,headers)
response = urllib2.urlopen(req)
page = response.read()
呼吁urlopen()
正在提高NameError: global name 'HTTPSConnectionV3' is not defined
。
import httplib
class HTTPSConnectionV3(httplib.HTTPSConnection):
def __init__(self, *args, **kwargs):
httplib.HTTPSConnection.__init__(self, *args, **kwargs)
def connect(self):
sock = socket.create_connection((self.host, self.port), self.timeout)
if self._tunnel_host:
self.sock = sock
self._tunnel()
try:
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, \
ssl_version=ssl.PROTOCOL_SSLv3)
except ssl.SSLError, e:
print("Trying SSLv3.")
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, \
ssl_version=ssl.PROTOCOL_SSLv23)
class HTTPSHandlerV3(urllib2.HTTPSHandler):
def https_open(self, req):
return self.do_open(HTTPSConnectionV3, req)
urllib2.install_opener(urllib2.build_opener(HTTPSHandlerV3()))
这确实会打印“尝试 SSLv3”并引发URLError: <urlopen error [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol>
https_sslv3_handler = urllib.request.HTTPSHandler(context=ssl.SSLContext(ssl.PROTOCOL_SSLv3))
opener = urllib.request.build_opener(https_sslv3_handler)
urllib.request.install_opener(opener)
但这引发了AttributeError: 'module' object has no attribute 'request'
。并且确实help(urllib)
不包括任何提及请求,并import urllib.request
导致No module named request
.
我在 Enthought Canopy 发行版中使用 Python 2.7.3。cPanel 站点使用的是自签名证书,我提到它是因为它是一种违规行为,会导致常规浏览器出错,尽管我认为 urllib 和 urllib2 实际上并没有对证书进行身份验证。
感谢您阅读,如果您有建议或可以帮助我理解问题,更是如此。