脚本无法发出POST请求

时间:2017-04-19 00:15:30

标签: python python-2.7 twisted treq

我有以下脚本:

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/ 它应该与请求很相似并基于扭曲。

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

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

在找到上述代码的错误时,我没有运气。

2 个答案:

答案 0 :(得分:2)

首先,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作为主要任务,将调用所有其他任务,完成后将指示反应堆停止,按{ {3}}:

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,[])

答案 1 :(得分:0)

问题是您没有正确使用reactor。注意我实际上并不知道这个反应堆做了什么,但我认为应用程序正常运行至关重要。

从在线发现的示例判断,您不是直接导入反应堆,而是创建twisted并通过方法回调将其传递给您。

作为示例(taken online并修改):

import json

from twisted.internet import defer, task
import treq

def response_recieved(response, password):
    response.json().addCallback(lambda payload: print(payload, password))

def main(reactor, *args):
    d = treq.post('http://httpbin.org/post',
                  json.dumps({"msg": "Hello!"}).encode('ascii'),
                  headers={b'Content-Type': [b'application/json']})
    d.addCallback(response_recieved, 42)
    return d

task.react(main, [])

运行后得到的输出是:

{'url': 'http://httpbin.org/post', 'json': {'msg': 'Hello!'}, 'files': {}, 'headers': {'Connection': 'close', 'Content-Type': 'application/json', 'Content-Length': '17', 'Accept-Encoding': 'gzip', 'Host': 'httpbin.org'}, 'origin': '127.0.0.1', 'args': {}, 'form': {}, 'data': '{"msg": "Hello!"}'} 42

使用python 2,要运行上面的示例,还需要在文件顶部包含以下import语句:

from __future__ import print_function