abort(400) raising 500 internal server error - Flask 1.0 - python

I have this function:
if not (payload.get('password') == payload.get('password_cpy')):
abort(400)
when I run the app with flask run and debug is off, I get a pretty print of a BadRequest error which in raw shows the following info:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not
understand.</p>
And on terminal I see:
127.0.0.1 - - [30/Apr/2018 00:28:20] "POST /sign_in HTTP/1.1" 400 -
When I run it and debug is on, the error I get is:
werkzeug.exceptions.BadRequest
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy)
sent a request that this server could not understand.
And in terminal I see a 500 error:
127.0.0.1 - - [30/Apr/2018 00:29:27] "POST /sign_in HTTP/1.1" 500 -
Why do I get two different behaviors? Is there a way in which I can get the 400 error even in debug mode?

try to update your Flask1.0 to Flask1.0.1
I guess the bug has been fixed in Flask1.0.1 :)
https://github.com/pallets/flask/issues/2735

Related

Python + Flask Facebook messenger bot doesn't work

I tried a bunch of tutorials, here is an example of one of them
https://nickmccullum.com/build-facebook-bot-python-flask/
Request is send:
127.0.0.1 - - [02/May/2022 10:18:15] "GET /?hub.mode=subscribe&hub.challenge=646416116&hub.verify_token=YOUR_VERIFY_TOKEN HTTP/1.1" 200 -
But when trying to validate, the Webhook throws this error:
Validation of the callback URL or confirmation token failed. Confirm the information provided or try again later.
what could be the problem

How to handle ah/start with python GAE app?

I had to change the scaling type on my GAE app to handle long-running requests.
But now the app has stopped responding, all I see are:
2021-04-09 01:22:59 default[20210409t011947] "GET /_ah/start HTTP/1.1" 200
2021-04-09 01:22:59 default[20210409t011947] "GET /_ah/start HTTP/1.1" 200
2021-04-09 01:23:00 default[20210409t011947] "GET /_ah/start HTTP/1.1" 200
2021-04-09 01:23:00 default[20210409t011947] "GET /_ah/start HTTP/1.1" 200
I understand I have to respond to this startup event with some type of 200 response which I am doing like this:
#app.route('/_ah/start')
def startup():
'''app starting'''
logging.info('handle _ah/start')
return {
'status': 'OK',
'msg': 'boot'
}
But I never see that endpoint actually get called. Instead i just get the logs like I'm not responding to it.
After some digging I found this message talking about how maybe traffic wasn't getting switched over between instances and use --promote to switch but doesn't work.
The main.py has some logging that should come out at startup but I don't see anything.
turns out there was a front-end proxy intercepting the events!

POST http://127.0.0.1:8000/ 400 (Bad Request)

I am trying to create a client and upload an image in Form React Component, when I send it to the backend using Axios I get the message "POST http://127.0.0.1:8000/ 400 (Bad Request)". If I create a client using postman, It creates it but when I do it in my React App, I get the 400 message. Does someone know what to do?

Google Drive Web hook don't work without any error

I use python and google-api-python-client==1.6.2 to work with user Google Drive's. Everything works perfectly without any bugs, but I didn't receive any push notification from Google Drive (Google Drive API Documentation). "Registering your domain" successfully completed. "Creating notification channels" completed with Channel object returning to me.
My code:
try:
drive_service.files().watch(
fileId='CPw3cbyqkoC1QMK48R24-Z2CG9w',
body=dict(
id=str(uuid.uuid4()),
resourceId='CPw3cbyqkoC1QMK48R24-Z2CG9w',
type='web_hook',
address='https://my-domain-address/web_hook'
)
).execute()
except HttpError as err:
logger.exception('HttpError {}: content={}'.format(err.uri, err.content))
Response:
{
u'resourceId': u'CPw3cbyqkoC1QMK48R24-Z2CG9w',
u'kind': u'api#channel',
u'expiration': u'1495448262000',
u'id': u'8837a4ad-98c0-4e89-8899-c07e12e3bffc',
u'resourceUri': u'https://www.googleapis.com/drive/v3/files/0B2lHB_g_GJY9RWx6UkRjWUFjSVU?acknowledgeAbuse=false&alt=json&supportsTeamDrives=false&alt=json'
}
After it there are not any requests to https://my-domain-address/web_hook when User modify watched Google Drive resource.
Have you any ideas what I do wrong?
Log messages linked with the problem that I found while search a solution:
64.233.172.141 - - [22/May/2017:09:11:10 +0000] "POST /web_hook HTTP/1.1" 403 807 "-" "APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)"
64.233.172.143 - - [22/May/2017:09:11:12 +0000] "POST /web_hook HTTP/1.1" 403 807 "-" "APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)"
64.233.172.143 - - [22/May/2017:09:11:13 +0000] "POST /web_hook HTTP/1.1" 403 807 "-" "APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)"
So I'm receiving the Google Drive requests to my webhook.
Firstly I think that Nginx is blocking them, but after I had seen that all requests are POST.
So I came to the problem of forwarding POST requests. Also I use Django and forgot disable CSRF protection to webhook. This is a solution of my problem.
I'll be glad if it helps someone.

Extract data from Get requests in python

I have got a SimpleHTTPServer in a raspberry pi working with a python script that is executed in the same directory as the index.html web page. The code is the following:
#!/usr/bin/python
import SimpleHTTPServer
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import httplib2
PORT = 8080
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
Once the web page is loaded, it starts to send different get requests with data that I need to read with my python script, but I don know how to do it.
This is an example of the get requests;
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /ok.png HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /red.png HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /arduino/start/0.16388046142178503 HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /favicon.ico HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:17] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:17] "GET /arduino/update/0.6913944096802204 HTTP/1.1" 404 -
Don't worry about the 404 error, it appears because the web has different icons that I did not put in the directory yet because I am doing tests.
The reason because I want to do that it's because I am "translating" a web sever to control a solenoid valve and different sensors that I had working on Arduino Yun. In arduino, the code reads the get requests, and depending on what is the get request asking for it responses with an XML package with the sensor data or it acts on the valve.
On arduino, the code for "catching" the get request as a String is the following;
if (client) { // got client?
String request = client.readString();
request.trim();
And then the program looks for a word in the string and depending on what word is found it responses with different information. An example;
if (request=="inicial") {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
// send XML file containing input states
inicial(client);
}
Summaraizing, I want to read the get requests as a String in my python script and extract data from them, and then response with an XML, but I think this last step will be better explained on the internet so I don't worry about it.
I hope my explanation was enough clear.
Thanks!!!
This is how you can make request to an address. The response object is assigned to the variable. Then you can extract anything you want from it.
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}

Categories

Resources