gpt4 book ai didi

python - 脚本无法发出 POST 请求

转载 作者:行者123 更新时间:2023-12-01 03:05:21 24 4
gpt4 key购买 nike

我有以下脚本:

from twisted.internet import reactor
import time,hashlib,urllib2,json,treq
from urllib import urlencode

#This is used to print out the Password that is found. This is called at the
# end of each loop. And checks if response code == 200.
def done(response):
if response.code == 200:
sys.stdout.write( Password + "FOUND" )
#The Password is basically a 4 digit number, the line under starts with number 0.
PasswordStart = 0
#This is an array to make sure that the script does not do double requests to the host.
executed = []
#Loop which runs the URL request 10000 times
while PasswordStart<9999:
#Checks if PasswordStart is in array
if PasswordStart not in executed and PasswordStart<9999:
#Since PasswordStart is not in array, it will add it to the array
#and then run the rest of the code.
executed.append(PasswordStart)
#Makes a variable of the time/date, used later in headers
Timing = time.strftime("%Y-%m-%dT%H:%M:%S.00+00:00")
#Just four variables for registration date, which is used later in
# the datas_p variable
YearRegD = time.strftime("%Y")
DateRegD = time.strftime("-%m-%d")
YearRegD2 = str((int(YearRegD)-1))
RegD = YearRegD2 + DateRegD
#UserAgent for the request
UserAgent = "Samsung Galaxy S8 - 7.0.0"
#Username for datas_p data later
UName = "JamesRicky"
#Makes the PasswordStart into 4 digits: 0 becomes 0000, 40 becomes 0040.
Password = str(PasswordStart).zfill(4)
#These two hashes my string and makes a variable with the hash,
# which is later used in the headers part of request
HASH = hashlib.md5()
HASH.update(time.strftime("%Y-%m-%dT%H:%M:%S.00+00:00")+UName+Password)

#Now the fun part, defines url for the post request
url = "http://example.com/user"
#JSON data for the POST request
datas_p = {'Username': UName, 'Password': Password, 'RegDate': RegD}
#URLencodes JSON - Not sure if this is needed or not
datas = urlencode(datas_p)

#The headers for the POST request
headers = ({
'User-Agent': [UserAgent],
'Date': [Timing],
'Secret-Key': [HASH.hexdigest()],
'Content-type': ['application/json'],
'Accept-encoding': ['gzip'],
'Accept': ['*/*'],

})

#Sends the treq.post request using the information from above (url, data, headers)
d = treq.post(url, data=datas, headers=headers)
#Adds call back in done def above.
d.addCallback(done)
#Adds up on the PasswordStart, so it tries another password for the
next request in the loop.
PasswordStart+=1

reactor.run()

当我运行它时,它会遍历循环,但发出 0 个请求(无论我放置了哪个主机)。这意味着 treq.post 请求有问题。

我在这里做错了什么?

编辑:这是treq的文档:https://treq.readthedocs.io/en/latest/它应该很像 requests 并且基于twisted。

编辑2:以下是treq如何发出请求的示例代码:

http://nullege.com/codes/show/src%40b%40t%40btcx-HEAD%40btcx%40btce.py/51/treq.post/python

在阅读上面的代码时,我没有找到我做错了什么。

最佳答案

首先,twisted 是一个服务器应用程序,因此必须进行日志记录才能了解发生了什么:

twisted.python.log.startLogging(sys.stdout)

现在,Password 未在 done() 执行的上下文中定义(或者可能是,但一旦 Deferred 正在执行)。您需要按照 How to pass extra arguments to callback register functions with twisted python api? 显式地将额外数据传递给回调。 :

def done(response,Password):
if response.code == 200:
sys.stdout.write( Password + "FOUND" )

<...>
d.addCallback(done,Password)

(请注意,对于变量,PEP8 recommends lowercase_with_underscores 命名约定,CamelCase 用于类型名称。我只是偏离它以符合您现有的代码。)

最后,没有 reactor.stop(),因此您的代码将无限期地运行。停止的一种方法是使用 twisted.internet.task.react 而不是 reactordefer.gatherResults 作为将调用所有其他的,完成后将发出 react 堆停止的信号,如stop a twisted reactor after a gatherResults has finished :

def main():
#checkPassword() returns a Deferred for trying the corresponding password
calls = [checkPassword(password) for password in ('%04d'%i for i in range(10000))]
d=defer.gatherResults(calls)
return d

twisted.internet.task.react(main,[])

关于python - 脚本无法发出 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43484198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com