Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查cpanel是否可以使用python2.7成功登录?_Python_Cpanel - Fatal编程技术网

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

如何检查cpanel是否可以使用python2.7成功登录?,python,cpanel,Python,Cpanel,我需要一个脚本,使它像一个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: wit

我需要一个脚本,使它像一个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()

我修复了您的代码,它将返回一个实际数组,其中包含一个布尔数组,指示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
了解更多详细信息

还要注意

“但它不起作用”


这不是一个问题

是的,但是我需要一个带有用户名和密码数据的cpanel登录,以确定登录是否成功,我使用
request.post()
只供您或其他人帮助修复没有用户名和密码数据的代码。在这里,我为您提供有效的登录名cpanel demo:login:
https://demo.cpanel.net:2083
username:
democom
password:
DemoCoA5620
I更新了答案以使用您给定的数据。登录正常,正确数据回复200,错误数据回复401。很高兴我能帮上忙。另一个节点:如果您没有任何具体的原因,请转到Python3.X,因为2.X很快就会被弃用。