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

2024-09-30 18:15:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个脚本,使它像一个cpanel检查器,有一个以上的网址和网址是存储在一个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()

Tags: 文件txturldataifdeflinepass
1条回答
网友
1楼 · 发布于 2024-09-30 18:15:13

我修复了您的代码,它将返回一个实际数组,其中包含一个表示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,我猜您实际上是想发布一些东西到那个url。你的代码不能这样做。如果您只想发送请求,请使用requests.get方法

有关更多详细信息,请参阅请求包的官方文档:https://2.python-requests.org/en/master/user/quickstart/#make-a-request

还要注意的是

"but it doesn't work"

这不是问题

相关问题 更多 >