[Twisted-Python] treq POST abborting with: err: ('Could not adapt', '{"....", "..."} <InterfaceClass twisted.web.iweb.IBodyProducer>)

steven meiers commercials24 at yahoo.de
Fri Jan 6 12:03:47 MST 2017


hi,


sending a simple post request with a body (cookies) results in a error
here.

im using python 3.x with the latest twisted and treq installed via pip
in a virtualenv.


the code gets a chatroom login page, extracts two values, gets the
cookies and logs a user into the chat. (incomplete)
thing is for being logged in i need to send my current phpsessionid in
the post request...



glyph (without seeing the the code or backtrace) suggested that:
"I think the issue might be that it doesn't support bytes() on py3,
it's registered against str() or something"


here is my code:

from myhttp import getLoginPage, login
from twisted.internet import reactor


def myPrint(result):
    print("result:", result)

def myError(failure):
    print("failure:", failure)

def lala():
    #d = getLoginPage()
    d = login()
    d.addCallback(myPrint)
    d.addErrback(myError)



if __name__ == '__main__':
    lala()
    reactor.run()






from myhttp import getLoginPage, login
from twisted.internet import reactor


def myPrint(result):
    print("result:", result)

def myError(failure):
    print("failure:", failure)

def lala():
    #d = getLoginPage()
    d = login()
    d.addCallback(myPrint)
    d.addErrback(myError)



if __name__ == '__main__':
    lala()
    reactor.run()
(venv) julius at t560:~/code/twisted/webchat-client$ cat myhttp.py 
from bs4 import BeautifulSoup
import random
from twisted.internet import reactor, defer
from twisted.web.client import getPage
import treq
import urllib




url = b'http://chatroom2000.de'
useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36".encode('utf-8')

cookies = {}    


@defer.inlineCallbacks
def getLoginPage():
    global cookies 
    response = yield treq.get(url)
    final_page = yield response.content()
    cookies = response.cookies()
    defer.returnValue(final_page)



@defer.inlineCallbacks
def login():
    response = yield getLoginPage()
    
    print(cookies)
    print("rsponse:", response)
    soup = BeautifulSoup(response, "lxml")
    hmac = soup.input["name"]
    key = soup.input["value"]
    print("hmac:key", hmac,key)
    username_list = ["Demetrius", "Tyrone", "Marshawn"]
    username = random.choice(username_list)
    body = urllib.parse.urlencode({hmac: key,
        'username': username, 'pw':'', 'gender':'m', 'aaaa':''})


    target_url = url + b"/?CheckUserName"

    
    headers = {'Content-Type': 'application/x-www-form-
urlencoded','cache-control': ['no-store, no-cache, must-revalidate,
pre-check=0, post-check=0, max-age=0']}
    
    import json 
    try:
        print("cookies2:", cookies)    
        somepage = yield treq.post(target_url, json.dumps({hmac: key,
"username": username, 'pw':'', 'gender':'m', 'aaaa':''}),
headers=headers, cookies=cookies )
    except Exception as err:
        print("err:", err)
    response = yield somepage.content()
    defer.returnValue(response)



if __name__ == '__main__':
    #url = 'http://chatroom2000.de'
    getPage(url)

    reactor.run()




and the error:
cookies2: <RequestsCookieJar[<Cookie
PHPSESSID=jevr00m25j0clfv6aibbgbro26 for chatroom2000.de/>, <Cookie
db1_cookie_test=1483729313 for chatroom2000.de/>]>
err: ('Could not adapt', '{"pw": "", "aaaa": "", "username":
"Demetrius", "cd5a6e4774acfd7e090467c9ed67f7e6": "1483729313",
"gender": "m"}', <InterfaceClass twisted.web.iweb.IBodyProducer>)
failure: [Failure instance: Traceback: <class 'UnboundLocalError'>:
local variable 'somepage' referenced before assignment
/home/julius/code/twisted/webchat-client/venv/lib/python3.5/site-
packages/twisted/internet/defer.py:457:callback
/home/julius/code/twisted/webchat-client/venv/lib/python3.5/site-
packages/twisted/internet/defer.py:565:_startRunCallbacks
/home/julius/code/twisted/webchat-client/venv/lib/python3.5/site-
packages/twisted/internet/defer.py:651:_runCallbacks
/home/julius/code/twisted/webchat-client/venv/lib/python3.5/site-
packages/twisted/internet/defer.py:1355:gotResult
--- <exception caught here> ---
/home/julius/code/twisted/webchat-client/venv/lib/python3.5/site-
packages/twisted/internet/defer.py:1299:_inlineCallbacks
/home/julius/code/twisted/webchat-client/myhttp.py:54:login





i followd some files in the traceback, but got nowhere :(




More information about the Twisted-Python mailing list