Python login to my cpanel with python
Question: I want to create a script with python that test if my combination (username
and password) is correct, but I always get a 401 HTTP response so i think the
script can't submit the login data. (the cpanel login isn't a traditional
login panel so i will use the demo login panel as our example-site.com) :

    
    
    import urllib, urllib2, os, sys, re
    site = 'http://cpanel.demo.cpanel.net/'
    username = 'demo'
    password = 'demo'
    headers = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0",
    "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Encoding" : "gzip, deflate",
    "Accept-Charset" : "ISO-8859-1,utf-8;q=0.7,*;q=0.7"}
    data = [
    ("user",username), 
    ("pass",password), 
    ("testcookie",1), 
    ("submit","Log In"), 
    ("redirect_to",'http://cpanel.demo.cpanel.net/'), 
    ("rememberme","forever")]
    
    req = urllib2.Request(site, urllib.urlencode(dict(data)), dict(headers))
    response = urllib2.urlopen(req)
    if any('index.html' in v for v in response.headers.values()) == True :
     print "Correct login" 
    else :
     print "incorrect"
    

I get this error :

    
    
    Traceback (most recent call last):
      File "C:\Python27\cp\cp4.py", line 19, in <module>
        response = urllib2.urlopen(req)
      File "C:\Python27\lib\urllib2.py", line 126, in urlopen
        return _opener.open(url, data, timeout)
      File "C:\Python27\lib\urllib2.py", line 400, in open
        response = meth(req, response)
      File "C:\Python27\lib\urllib2.py", line 513, in http_response
        'http', request, response, code, msg, hdrs)
      File "C:\Python27\lib\urllib2.py", line 438, in error
        return self._call_chain(*args)
      File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
        result = func(*args)
      File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 401: Access Denied
    

Any ideas to solve the problem and test the login details?


Answer: Consider using [Requests](http://python-requests.org), a much more user-
friendly HTTP client library for Python.

    
    
    import requests
    
    url = 'http://cpanel.demo.cpanel.net/login/'
    username = 'demo'
    password = 'demo'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0',
    }
    data = {
        'user': username,
        'pass': password,
    }
    
    response = requests.post(url, headers=headers, data=data)
    if response.status_code == 200:
        print "Successfully logged in as {username}".format(username=username)
    else:
        print "Login unsuccessful: HTTP/{status_code}".format(status_code=response.status_code)
    

Edited to check for HTTP/200, as CPanel does throw an HTTP/401 if the login is
incorrect.

