如何将用户重定向到返回的 URL python

How to redirect user to a returned URL python

所以我正在测试 paypal rest sdk 并使用 python 作为我的 cgi 语言(我知道它也是 2007 年的),无论如何我遇到了障碍,因为我无法让它重定向到返回的 URL 页面。

我已经尝试过 print Location 方法。它对我不起作用。

# Create Payment Using PayPal Sample
# This sample code demonstrates how you can process a
# PayPal Account based Payment.
# API used: /v1/payments/payment
from paypalrestsdk import Payment
import logging

logging.basicConfig(level=logging.INFO)

# Payment
# A Payment Resource; create one using
# the above types and intent as 'sale'
payment = Payment({
"intent": "sale",

# Payer
# A resource representing a Payer that funds a payment
# Payment Method as 'paypal'
"payer": {
    "payment_method": "paypal"},

# Redirect URLs
"redirect_urls": {
    "return_url": "http://localhost:3000/payment/execute",
    "cancel_url": "http://localhost:3000/"},

# Transaction
# A transaction defines the contract of a
# payment - what is the payment for and who
# is fulfilling it.
"transactions": [{

    # ItemList
    "item_list": {
        "items": [{
            "name": "item",
            "sku": "item",
            "price": "5.00",
            "currency": "USD",
            "quantity": 1}]},

    # Amount
    # Let's you specify a payment amount.
    "amount": {
        "total": "5.00",
        "currency": "USD"},
    "description": "This is the payment transaction description."}]})

 # Create Payment and return status
 if payment.create():
 print("Payment[%s] created successfully" % (payment.id))
  # Redirect the user to given approval url
for link in payment.links:
    if link.method == "REDIRECT":
        # Convert to str to avoid google appengine unicode issue
        # https://github.com/paypal/rest-api-sdk-python/pull/58
        redirect_url = str(link.href)
        print("Redirect for approval: %s" % (redirect_url))
else:
    print("Error while creating payment:")
    print(payment.error)

因此,如果你们这些新手在用户浏览器上抛出 301 或 302,那真的对我有帮助。谢谢。

我自己解决了。通过将此添加到代码

print 'Content-Type: text/html'
print 'Location: %s' % redirectURL
print # HTTP says you have to have a blank line between headers and content
print '<html>'
print '  <head>'
print '    <meta http-equiv="refresh" content="0;url=%s" />' %redirectURL
print '    <title>You are going to be redirected</title>'
print '  </head>' 
print '  <body>'
print '    Redirecting... <a href="%s">Click here if you are not redirected</a>' % redirectURL
print '  </body>'
print '</html>'

知道脚本重定向页面就像 charm.Thank 你