我正在学习 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 都需要是“get(self)”,这样我就不会收到 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)
谢谢 :)