Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a pile of tasks to automate within cPanel. There is a cPanel API described at http://videos.cpanel.net/cpanel-api-automation/ but I tried what I thought was easier for me...

  • Based on an answer from skyronic at How do I send a HTTP POST value to a (PHP) page using Python? I tried

    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()
    

    The call to urlopen() is raising NameError: global name 'HTTPSConnectionV3' is not defined.

  • So then based on http://bugs.python.org/issue11220 I tried preceding the code above with

    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()))
    

    This does print the "Trying SSLv3" and raises URLError: <urlopen error [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol>

  • And finally that led me to https://github.com/kennethreitz/requests/issues/606 where gregakespret who say he solved a similar problem using a solution from Senthil Kuaran at http://bugs.python.org/issue11220 :

    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)
    

    But that raises AttributeError: 'module' object has no attribute 'request'. And indeed help(urllib) doesn't include any mention of request, and import urllib.request results in No module named request.

    I'm using Python 2.7.3 within the Enthought Canopy distribution. The cPanel site is using a self-signed certificate, which I mention since it'sa an irregularity that would trip up a regular browser, though I gather that urllib and urllib2 don't actually authenticate the certificate anyway.

    Thank you for reading, more so if you have a suggestion or can help me understand the problem.

  • share|improve this question
        
    urllib does not have HTTPSHandler. Refer to this. You can use urllib2 instead. Check this –  smitrp Jul 4 '13 at 8:55

    1 Answer 1

    I would use the requests library.

    I'm the OP and it's been a while since I posted this. I've solved other related tasks (POST to use Instructure's Canvas API) using the requests library and found that code that had worked with urllib/urllib2 is now much shorter and sweeter.

    Someone just upvoted this question, causing me to see that noone had answered it. My answer isn't much of one to my OP, but it is the direction I'd advise, having solved related problems since the post.

    As for this question, I solved the problem by scripting in BASH on the server that had cPanel running. It was just a matter of identifying which cPanel scripts to call. But I did not get it running through the cPanel Web API.

    share|improve this answer

    Your Answer

     
    discard

    By posting your answer, you agree to the privacy policy and terms of service.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.