Python sends and receives HTTP POST

I am learning Python and I am trying to do something very simple: send an HTTP POST from one application and get it in another, not only can I not make it work, I cannot make it work with what would seem reasonable using def post (self). This is the code that I have that does not give errors, but does not complete the task: Sender application:

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) 

Receiving application:

 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) 

All I get on localhost is β€œyou sent:” :( Worst of all, I don’t understand why both defs should be β€œget (self)”, so I don’t get 405 error ... Thanks to everyone :)

This is the "new" code, no changes for the sender:

 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) 

The recipient, I went to the post, as Sam said, but I get 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) 

Thanks:)

+4
source share
3 answers

Your last comments mention 3 points of contact:

  • application-1 responds to GET POSTing requests on application-2
  • application-2 responds to POST POSTing requests before application-3
  • application-3 responds to POST requests displaying responses to the screen

If you should have all the work and messaging on the server side, I would suggest using the App Engine URL Fetch service from application-1 to issue a POST request to application-2 , and then 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 .

Server side example

 # 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 main disadvantage of this is the initial GET request, which will not receive a response until both consecutive POST requests return. This risks a high response time.

Client Side Example

This version can be executed using XMLHttpRequest from the client. The advantage here is that the client receives an immediate response from the initial GET request, and subsequent POST requests are processed in the client browser. application-2 and application-3 should respond in the same way. Only application-1 changes and it will just be necessary to serve the following HTML and Javascript for the client, as in this example .

+1
source

Check out this example :

 self.response.write("<html><body><p>Hi there!</p></body></html>") 

The response buffers all the output in memory, and then sends the final result when the handler exits. webapp2 does not support streaming data for the client.

so basically, response.write should be the last thing you call:

 def get(self): con = self.request.get("message") self.response.write("you sent: " + con ) 

In addition, I suggest you check out this link to learn more about POST and GET requests with forms in Appengine. I do not understand what you are trying to do with these two views, but they collide with each other.

0
source

I'm new too. Learning Python from last weekend, and your question has been a reference for my training.

Sending application

==============================

 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) 

Receiving application

 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) 
0
source

Source: https://habr.com/ru/post/1489144/


All Articles