Python发送和接收HTTP POST
|
我正在学习
Python,我正在尝试做一些非常简单的事情:从一个应用程序发送HTTP POST并在另一个应用程序中接收它,不仅我无法让它工作,我无法得到它使用def post(self)来处理看似合理的事情.这是我的代码,它不会给出错误,但也不执行任务:
发件人申请: import cgi
import webapp2
import urllib
import urllib2
import json
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
senddata = {}
senddata["message"] = 'Testing the Sender'
class MainPagePost(webapp2.RequestHandler):
def get(self):
txt_url_values = urllib.urlencode(senddata)
txturl = 'http://localhost:10080'
result = urllib.urlopen(txturl,txt_url_values)
self.redirect('http://localhost:10080')
application = webapp2.WSGIApplication([
('/',MainPagePost),],debug=True)
接收申请: import cgi
import webapp2
import urllib
import urllib2
import json
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
class MainPageGet(webapp2.RequestHandler):
def get(self):
self.response.write('you sent:')
con = self.request.get("message")
self.response.write(con)
application = webapp2.WSGIApplication([
('/',MainPageGet),debug=True)
我在本地主机上获得的是“你发送的:”:( 这是“新”代码,对于发件人没有变化: import cgi
import webapp2
import urllib
import urllib2
import json
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
senddata = {}
senddata["message"] = 'Testing Tester'
class MainPagePost(webapp2.RequestHandler):
def get(self):
txt_url_values = urllib.urlencode(senddata)
txturl = 'http://localhost:10080'
result = urllib.urlopen(txturl,debug=True)
正如Sam建议的那样,接收器改为发布,但我得到了405: # -*- coding: utf-8 -*-
import cgi
import webapp2
import urllib
import urllib2
import json
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
class MainPageGet(webapp2.RequestHandler):
def post(self):
# self.response.write('you sent:')
con = self.request.get("message")
self.response.write('you sent: ' + con)
application = webapp2.WSGIApplication([
('/',debug=True)
谢谢 :) 解决方法
您的上次评论提到3个联系人:
> application-1响应GET请求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,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:n{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([
('/',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([
('/',debug=True)
这样做的主要缺点是,在两个连续的POST请求都返回之前,初始GET请求不会收到响应.这可能会导致高响应时间. 客户端示例 该版本可以使用 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |








