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

how to check if cpanel can login successfully with python2.7?

我需要一个脚本来使它像一个 cpanel 检查器,有超过 1 个 url 并且 url 存储在一个 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()

我以某种方式修复了您的代码,它将 return 包含布尔数组的实​​际数组,指示 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 我猜你实际上想要 POST 一些东西到那个网址。您的代码不会那样做。如果您只想发送请求,请使用 requests.get 方法。

请参阅请求数据包的官方文档:https://2.python-requests.org/en/master/user/quickstart/#make-a-request了解更多详情。

另请注意

"but it doesn't work"

不是问题。