我正在学习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)
我在本地主机上获得的是“你发送的:”:( 最糟糕的是,我不明白为什么两个def都需要“得到(自我)”,这样我就不会得到405错误...... 谢谢大家:))
这是“新”代码,对于发件人没有变化:
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, txt_url_values)
self.redirect('http://localhost:10080')
application = webapp2.WSGIApplication([
('/', MainPagePost),
], 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([
('/', MainPageGet),
], debug=True)
谢谢:)
答案 0 :(得分:1)
Your last comments mentions 3 points of contact:
application-1
responding to GET requests POSTing to
application-2
application-2
responding to POST requests POSTing
to application-3
application-3
responding to POST requests
showing responses to screenIf you must have all the work and message passing take place on the server side, I would suggest using App Engine's URL Fetch service from application-1
to issue a POST
request to application-2
followed by a POST
request to application-3
. This is because you cannot reliably redirect from application-2
to application-3
with a server-initiated POST
request based on how most browsers implement redirection.
# 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:\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([
('/', 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)
The major downside to this is the initial GET request will not receive a response until both sequential POST requests have returned. This risks high response times.
This version could be accomplished using XMLHttpRequest
from the client. The advantage here is that the client gets an immediate response from the initial GET
request while the subsequent POST
requests are treated in the client's browser. application-2
and application-3
should serve responses in the same way. Only application-1
changes and would simply need to serve the following HTML and Javascript to the client as in this example.
答案 1 :(得分:0)
检查example:
self.response.write("<html><body><p>Hi there!</p></body></html>")
响应缓冲内存中的所有输出,然后在处理程序退出时发送最终输出。 webapp2不支持将数据流传输到客户端。
所以基本上,response.write必须是你调用的最后一件事:
def get(self):
con = self.request.get("message")
self.response.write("you sent: " + con )
另外,我建议您查看此link以了解有关在Appengine上使用表单的POST和GET请求的更多信息。我不明白你要对这两种观点做些什么,但它们相互冲突
答案 2 :(得分:0)
我也是新手。从上周末学习Python,你的问题一直是我学习的参考。
============================
import webapp2
import urllib
import urllib2
import json
import os
import random
import time
i=0
class MainHandler(webapp2.RequestHandler):
def get(self):
url = "http://localhost:12080/"
response = urllib2.urlopen(url)
html_string = response.read()
self.response.write(html_string)
self.response.write(os.environ.get("HTTP_USER_AGENT", "N/A"))
self.response.write(random.uniform(1,10))
"""
while True:
self.post()
global i
i+=1
time.sleep(2)
"""
def post(self):
url = "http://localhost:12080/receive"
name = random.uniform(1,10)
post_data_dictionary = {'name':str(name), "age":i, "favorite OS":"Ubuntu"}
http_headers = {'User-Agent':'OWN'}
post_data_encoded = urllib.urlencode(post_data_dictionary)
request_object = urllib2.Request(url, post_data_encoded,http_headers)
response = urllib2.urlopen(request_object)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
import webapp2
import cgi
import webapp2
import urllib
import urllib2
import json
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext import db
import os
class Message(db.Model):
msg=db.StringProperty()
#user_agent=db.StringProperty()
age=db.StringProperty()
fOS=db.StringProperty()
useragent=db.StringProperty()
class Receive(webapp2.RequestHandler):
#def get(self):
#self.response.write('Rececive!')
#self.post()
def post(self):
var1 = self.request.get("name")
var2 = self.request.get("age")
var3 = self.request.get("favorite OS")
var4 = os.environ.get("HTTP_USER_AGENT")
mes=Message()
mes.msg=var1
mes.age=var2
mes.useragent=var4
mes.fOS=var3
mes.put()
#self.response.write('you sent: ' + con)
class MainHandler(webapp2.RequestHandler):
def get(self):
#req=datastore.RunQueryRequest()
#gql_query= req.gql_query
self.response.write(os.environ.get("HTTP_USER_AGENT"))
#a=Message()
app = webapp2.WSGIApplication([
('/', MainHandler),
('/receive', Receive)
], debug=True)