用python生成结构化文本的想法或模块?

2024-10-01 02:28:37 发布

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

首先,我很抱歉,因为我不知道怎么问我的问题,上次在一个安全挑战中,我试图用curl发送一些请求,在他们进行了大量测试以了解挑战是如何工作的几分钟之后,我尝试编写一些python代码来自动生成我的请求,并赢得了一些时间

以下是我曾经尝试过的一些请求: 最基本的

curl http://10.20.0.50:80/

然后我必须指定路径示例:

curl http://10.20.0.50:80/transfert/solde
curl http://10.20.0.50:80/account/creat
...

有时添加授权或cookie。。。你知道吗

curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="  -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" 

或添加一些参数:

curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="  -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" --data-raw '{"id":"521776"}' -v

所以问题是,我必须测试很多东西,有没有授权,有没有cookie,更改cookie几次,添加--原始数据。。。我试着写一个剧本来帮我做这个,但很难看:

url = "http://10.20.0.50:80/"
auth = ' -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="'
def generate(path,c=None,h=True,plus = None):
    #c cookie , h if we put authentification
    #plus add more code at the end of the request
    global auth  # authentification
    global url
    if c:
    cook = ' -H "cookie: PHPSESSID={};"'.format(c)
    req = "curl "+url+path 
    if h:#h bool
        req += auth
    if c :
        req += cook
    if plus :
        req += plus
    req+=" -v "
    return req

为了可读性,我去掉了一个参数——数据行,我想知道是否有更好的方法!不仅仅是这个例子,一般来说,如果我想创建python代码,生成类的代码源,我必须指定类的名称,属性和类型,代码生成一个模板。。。你知道吗

我希望你能帮助我 PS:如果我犯了一些错误,我的英语很抱歉


Tags: 代码authhttpurlifbasiccookieplus
1条回答
网友
1楼 · 发布于 2024-10-01 02:28:37

或许,一种“改进”代码的方法是这样做:

def generate(command = "", headers = [], raws = [], other = [], v = True):
    if headers:
        command += "".join(" -H " + k for k in h)
    if raws:
        command += "".join("  data-raw " + k for k in raw)
    if v:
        command += " -v"
    if other:
        command += "".join(" " + k for k in other)
    return command

h = ['"Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="', '"cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;"']
raw = ["'{\"id\":\"521776\"}'"]
cmd = "curl http://10.20.0.50:80/transfert/solde"

command1 = generate(command=cmd,headers=h,raws= raw)
command2 = generate(command=cmd,headers=h,raws=raw, v=False)
command3 = generate(command=cmd,v = False)

print("command1:",command1)
print("command2:", command2)
print("command3:", command3)

输出:

command1: curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;"  data-raw '{"id":"521776"}' -v
command2: curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;"  data-raw '{"id":"521776"}'
command3: curl http://10.20.0.50:80/transfert/solde

相关问题 更多 >