我正在尝试编写一个脚本,其中某人可以在一个框中键入网站的名称,我的脚本将呈现该网站的资源。我不知道如何去做,我认为它会像这样:
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()
这个脚本无效,当脚本出现错误时:Request did not return bytes
。有谁能告诉我我做错了什么或者我可以在哪里了解更多关于这个主题的内容?谢谢!!
答案 0 :(得分:1)
我暂时没有使用过Resources和Site对象,但我确信你已经重载var WePay = require('machinepack-wepay');
// ... extraneous code removed for brevity
var member = req.session.member;
if( !_.has( member, 'wepay' ) ) {
WePay.userRegister({
clientId: config.wepay_client_id,
clientSecret: config.wepay_client_secret,
email: member.email,
scope: 'manage_accounts,collect_payments,view_user,send_money',
firstName: member.firstName,
lastName: member.lastName,
originalIp: req.headers['x-forwarded-for'],
originalDevice: req.headers['user-agent'],
tosAcceptanceTime: Math.floor(new Date() / 1000),
callbackUri: config.site_url + '/wepay/user?member=' + member.id,
useProduction: isProd
}).exec({
error: function (err) {
yourErrorHandler(err);
},
success: function (result) {
Member.update({id: member.id}, {wepay: result}, function (err, updated) {
if (err) {
yourErrorHandler(err);
}
else {
member = updated[0];
WePay.accountCreate({
accessToken: member.wepay.access_token,
name: 'Account Name',
description: 'My new account'
}).exec({
error: function (err) {
yourErrorHandler(err);
},
success: function (result) {
Member.update({id: member.id}, {wepay_account: result}, function (err, updated) {
if (err) {
sails.log.error("error updating page:", err);
}
req.session.member = updated[0];
// PATTERN CONTINUES HERE
});
}
});
}
});
}
});
}
else{
WePay.userDetails({
accessToken: member.wepay.access_token,
useProduction: false,
}).exec({
error: function (err){
yourErrorHandler(err);
},
success: function (result){
_.extend( member.wepay, result );
Member.update({id: req.session.current_page.id}, member, function (err, updated) {
if (err) {
sails.log.error("error updating page:", err);
}
req.session.member = updated[0];
// PATTERN CONTINUES HERE
});
},
});
}
方法并返回Resource.getChild()
来实现你想要的。如果你走那条路,我认为它会有点混乱。但是,在ReverseProxyResource
中,您尝试做的事情是微不足道的,可以很容易地解决。您基本上设置了klein
,这使得可以返回branch=True
对象。例如:
Resource
现在最后,你在Python 3.x上运行它,似乎from klein import Klein
from 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 you return Resources
answer = request.args.get(b'form-field', b'localhost').decode('utf-8') # also use request.args instead
return ReverseProxyResource(answer, 80, '')
app.run('localhost', 80)
尚未完全移植。因此,如果您在Python 3中运行此代码,您将获得回溯。