linux - How do I handle POST requests in Twisted? -
i'm trying write script can type name of website box , script render resources of website. i'm not sure how go it, i'm thinking this:
class formpage(resource): isleaf = true def render_get(self, request): return b"""<html><body><form method="post"><input name="form-field" type="text"/><input type="submit" /></form></body></html>""" def render_post(self, request): answer = request.content.read()[11:].decode() reverseproxyresource(answer, 80, b'') factory = site(formpage()) reactor.listentcp(80, factory) reactor.run()
this script isn't working, when script error: request did not return bytes
. tell me i'm doing wrong or can learn more subject? thanks!!
i haven't worked resources , site objects in while, i'm pretty sure have overload resource.getchild()
method , return reverseproxyresource
achieve want. can bit messy in opinion if go route. however, in klein
you're trying trivial , can solved. set branch=true
, makes resource
object can returned. example:
from klein import klein twisted.web.proxy import reverseproxyresource app = klein() @app.route('/', methods=['get']) def render_get(request): return b"""<html><body><form method="post"><input name="form-field" type="text"/><input type="submit" /></form></body></html>""" @app.route('/', methods=['post']) def render_post(request, branch=true): # branch=true lets return resources answer = request.args.get(b'form-field', b'localhost').decode('utf-8') # use request.args instead return reverseproxyresource(answer, 80, '') app.run('localhost', 80)
now final matter, you're running on python 3.x , seems reverseproxyresource
hasn't been ported on yet. if run code in python 3, tracebacks.