如何检查cpanel是否可以使用python2.7成功登录?

时间:2019-09-05 16:01:00

标签: python cpanel

我需要一个脚本来像cpanel checker一样,具有超过1个网址,并且该网址存储在txt文件中。

用法:python script.py list.txt

文件list.txt中的格式:https://demo.cpanel.net:2083|democom|DemoCoA5620

这是我的代码,但是它不起作用,有人可以帮我吗?

谢谢。

import requests, sys
from multiprocessing.dummy import Pool as ThreadPool

try:
    with open(sys.argv[1], 'r') as f:
        list_data = [line.strip() for line in f if line.strip()]
except IOError:
    pass

def cpanel(url):

    try:

        data = {'user':'democom', 'pass':'DemoCoA5620'}

        r = requests.post(url, data=data)

        if r.status_code==200:

            print "login success"

        else:

            print "login failed"

    except:
        pass


def chekers(url):

    try:
        cpanel(url)
    except:
        pass

def Main():
    try:
        start = timer()
        pp = ThreadPool(25)
        pr = pp.map(chekers, list_data)
        print('Time: ' + str(timer() - start) + ' seconds')
    except:
        pass

if __name__ == '__main__':
    Main()

1 个答案:

答案 0 :(得分:0)

我以某种方式修复了您的代码,它将返回一个包含布尔数组的实​​际数组,该数组指示cpanel函数的成功。

from __future__ import print_function
import requests
from multiprocessing.pool import ThreadPool
try:
    list_data = ["https://demo.cpanel.net:2083|democom|DemoCoA5620",
                 "https://demo.cpanel.net:2083|UserDoesNotExist|WRONGPASSWORD",
                 ]
except IOError:
    pass

def cpanel(url):

    try:
        # try to split that url to get username / password

        try:
            url, username, password = url.split('|')
        except Exception as e:
            print("Url {} seems to have wrong format. Concrete error: {}".format(url, e))
            return False

        # build the correct url
        url += '/login/?login_only=1'

        # build post parameters
        params = {'user': username,
                  'pass': password}

        # make request
        r = requests.post(url, params)

        if r.status_code==200:

            print("login for user {} success".format(username))
            return True

        else:

            print("login for user {} failed due to Status Code {} and message \"{}\"".format(username, r.status_code, r.reason))
            return False

    except Exception as e:
        print("Error occured for url {} ".format(e))
        return False


def chekers(url):
    return cpanel(url)


def Main():
    try:
        # start = timer()
        pp = ThreadPool(1)
        pr = pp.map(chekers, list_data)
        print(pr)
        # print('Time: ' + str(timer() - start) + ' seconds')
    except:
        pass

if __name__ == '__main__':
    Main()

输出:

login for user democom success
login for user UserDoesNotExist failed due to Status Code 401 and message "Access Denied"
[True, False]

请注意,我用一些固定的url代替了文件读取操作。

自从您使用request.post以来,我猜您实际上是想向该网址发布内容。您的代码不这样做。如果只想发送请求,请使用requests.get方法。

有关请求数据包,请参见官方文档:https://2.python-requests.org/en/master/user/quickstart/#make-a-request,以获取更多详细信息。

还要注意

  

“但它不起作用”

不是问题。