【问题标题】:Redirecting to other url in Pyramid?重定向到金字塔中的其他网址?
【发布时间】:2013-05-22 22:31:35
【问题描述】:

我正在制定 2 路短信服务。

用户将向 SMS 服务器平台 (www.sms.com) 提供的虚拟号码发送 SMS。该 SMS 服务器平台会将用户 SMS 数据传递到我的 url (http://www.yourdomainname.com/ReceiveSMS?from=from&message=message)

现在我根据“消息”处理用户请求,然后我需要回复这个网址 (www.sms.com/optin.php?user=username&pass=password&to=to_mobile_number&message=dynamic_message)

我的问题是如何在处理后将其发布到网址 www.sms.com/optin.php?user=username&pass=password&to=to_mobile_number&message=dynamic_message。

我想到的一种方法是使用 HTTPFound。

想知道是否有更有效的方法?

【问题讨论】:

  • 您的术语难以解析。您需要将用户重定向到该 url,还是需要在后台与该 url 对话?您不能在重定向中“发布”,因此您的要求不清楚。

标签: python post redirect pyramid pylons


【解决方案1】:

在金字塔视图中,您可以返回 webob 响应以绕过任何渲染器逻辑。因此,对于重定向,您将状态设置为 301/302location header

from webob import Response

@view_config(...)
def your_view(context, request):
    # do stuff
    return Response(status_int=302, location="http://goherenext.com")

HTTPFound 只是带​​有status hard-coded 的响应的子类。

【讨论】:

    【解决方案2】:

    Requests 非常适合发送 POST 请求

    >>> message = 'hello!' # From your inbound SMS
    >>> data = {
        'user': 'username', 
        'pass': 'password', 
        'message': message, 
        'to': '123456789'
    }
    >>> r = requests.post("www.sms.com/optin.php", params=data)
    

    【讨论】:

    • 感谢 Alex 回答我的问题。但是在这里,来自 SMS 服务器的请求将出现在我的视图中,视图处理完请求后,响应将转到“www.sms.com/optin.php”。只是不知道如何使用 Pyramid
    猜你喜欢
    • 2022-08-10
    • 2013-12-25
    • 2012-11-18
    • 2018-12-24
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2012-12-29
    • 2018-03-13
    相关资源
    最近更新 更多