python发送post_Python发送和接收HTTP POST

您的上次评论提到3个联系人:

> application-1响应GET请求POST到

应用-2-

> application-2响应POST请求POST

申请-3

> application-3响应POST请求

显示对屏幕的响应

如果您必须在服务器端完成所有工作和消息传递,我建议使用来自application-1的App Engine的URL Fetch服务向application-2发出POST请求,然后向application-3发出POST请求.这是因为根据most browsers implement redirection的方式,您无法使用服务器启动的POST请求从application-2可靠地重定向到application-3.

服务器端示例

# Application 1

import webapp2

import urllib

from google.appengine.api import urlfetch

url_app_2 = 'http://application-2.com/'

url_app_3 = 'http://application-3.com/'

class MainPage(webapp2.RequestHandler):

def get(self):

data_to_post = {

'message': 'Important data to pass on'

}

encoded_data = urllib.urlencode(data_to_post)

# Send encoded data to application-2

result = urlfetch.fetch(url_app_2, encoded_data, method='POST')

data_to_post = {

'message': result.content

}

encoded_data = urllib.urlencode(data_to_post)

# Send encoded application-2 response to application-3

result = urlfetch.fetch(url_app_3, encoded_data, method='POST')

# Output response of application-3 to screen

self.response.headers['Content-Type'] = 'text/plain'

self.response.write(result.content)

app = webapp2.WSGIApplication([

('/', MainPage),

], debug=True)

# Application 2

import webapp2

response_template = 'The message sent was:

{0}'

class MainPage(webapp2.RequestHandler):

def post(self):

message = self.request.get('message')

self.response.headers['Content-Type'] = 'text/plain'

self.response.write(response_template.format(message))

app = webapp2.WSGIApplication([

('/', MainPage),

], debug=True)

# Application 3

import webapp2

class MainPage(webapp2.RequestHandler):

def post(self):

message = self.request.get('message')

self.response.headers['Content-Type'] = 'text/plain'

self.response.write(message)

app = webapp2.WSGIApplication([

('/', MainPage),

], debug=True)

这样做的主要缺点是,在两个连续的POST请求都返回之前,初始GET请求不会收到响应.这可能会导致高响应时间.

客户端示例

该版本可以使用XMLHttpRequest从客户端完成.这里的优点是客户端从初始GET请求获得立即响应,而后续POST请求在客户端的浏览器中处理. application-2和application-3应该以相同的方式提供响应.只有application-1更改,并且只需要像this example那样向客户端提供以下HTML和Javascript.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值