How to send multiple post requests in same connection using python requests - python

In order to automate user registration in my website, I am using
>>> r = requests.post('http://www.dakshnetworks.com:8888/accounts/signup/', data=payload)
That is working well. but once the user is registered, i am directing him to another page where he has to set his workplace. So i tried to use
o= requests.post('http://www.dakshnetworks.com:8888/set/', data=payload2)
but that is not able to set the workplace of the user returning error 500
[01/Jun/2015 17:11:07] "POST /accounts/signup/ HTTP/1.1" 302 0
[01/Jun/2015 17:11:07] "GET / HTTP/1.1" 302 0
[01/Jun/2015 17:11:07] "GET /set/ HTTP/1.1" 200 26351
[01/Jun/2015 17:14:40] "POST /set HTTP/1.1" 500 75469
[01/Jun/2015 17:16:16] "POST /set/ HTTP/1.1" 500 126509
So how do i maintain the connection so that the server may know that the requests are coming from the same user.

Use a Session() object to maintain cookies, so that the server knows it is still the same user:
session = requests.Session()
r = session.post('http://www.dakshnetworks.com:8888/accounts/signup/', data=payload)
o = session.post('http://www.dakshnetworks.com:8888/set/', data=payload2)

Related

Why is the Flask 'POST' method not working?

I'm trying to create a small chatting platofrm on Flask. Nothing fancy, just an experiment. My problem is that flask doesn't seem to be taking any input from my post statements, even though I get the message in the console.
text = {"text":typed} #both userenter and typed are strings
username = {"username":userenter}
requests.post(send_url, username)
requests.post(send_url, text)
typed = ''
That's the most important part of the client code, as the rest is mainly getting things printed and getting keystrokes.
This is the flask code:
chat = {"Chat":["Welcome to my chatting thing!\n"]}
#app.route('/chat', methods = ['POST'])
def chatt():
global chat
username = request.form['username']
Text = request.form['text']
chat["Chat"].append(username)
chat["Chat"].append(": ")
chat["Chat"].append(Text)
chat["Chat"].append("\n")
return
For the flask code, my main goal is to take the input from the post and append it to the list within the dictionary 'chat'
Console logs:
172\.31.128.1 - - \[16/Feb/2023 23:07:42\] "GET /get-chat HTTP/1.1" 200 -
172\.31.128.1 - - \[16/Feb/2023 23:07:43\] "GET /get-chat HTTP/1.1" 200 -
172\.31.128.1 - - \[16/Feb/2023 23:07:44\] "GET /get-chat HTTP/1.1" 200 -
172\.31.128.1 - - \[16/Feb/2023 23:07:45\] "POST /chat HTTP/1.1" 400 -
172\.31.128.1 - - \[16/Feb/2023 23:07:45\] "POST /chat HTTP/1.1" 400 -
172\.31.128.1 - - \[16/Feb/2023 23:07:46\] "GET /get-chat HTTP/1.1" 200 -
172\.31.128.1 - - \[16/Feb/2023 23:07:47\] "GET /get-chat HTTP/1.1" 200 -
172\.31.128.1 - - \[16/Feb/2023 23:07:48\] "GET /get-chat HTTP/1.1" 200 -
172\.31.128.1 - - \[16/Feb/2023 23:07:49\] "GET /get-chat HTTP/1.1" 200 -
The console does register that the posts got through, but nothing was added to the list.
I've tried playing around with the code and trying out different syntaxes, as well as printing it out. However, no matter what I've tried, it feels like the console is the only proof that the POST statements went through.
Edit #1:
I have tried using debuggers such as breakpoint() and some others. Didn't work.
Also, somebody pointed out that my post was returning 400 instead of 200, which is a malformed request. If anybody knows a solution to that, please tell me.
Tell me if you need any more information about my code if you need it.
There is 400 error, check what that means
You're sending two requests instead of one. Your action is expecting 2 parameters:
data = {"text": typed, "username": userenter}
requests.post(send_url, data)
You may also want to receive some response, so action can return something that you can pick up.

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!

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.

Invalid request state: oauth2 flask

I am using the following code for authentication using oauth 2.0
oauth2.init_app(
app,
scopes=['email', 'profile'],
authorize_callback=_request_user_info)
The route is as follows
#app.route('/')
#oauth2.required
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello ' + session['profile']['displayName']
The auth callback method
def _request_user_info(credentials):
"""
Makes an HTTP request to the Google+ API to retrieve the user's basic
profile information, including full name and photo, and stores it in the
Flask session.
"""
http = httplib2.Http()
credentials.authorize(http)
resp, content = http.request(
'https://www.googleapis.com/plus/v1/people/me')
if resp.status != 200:
current_app.logger.error(
"Error while obtaining user profile: %s" % resp)
return None
session['profile'] = json.loads(content)
I can see the consent screen and then after that there are too many redirects. The logs shows the following.
NFO:werkzeug:127.0.0.1 - - [10/Apr/2016 15:25:21] "GET / HTTP/1.1" 302 -
INFO:werkzeug:127.0.0.1 - - [10/Apr/2016 15:25:21] "GET /oauth2authorize?scopes=profile&scopes=email&return_url=http%3A%2F%2F127.0.0.1%3A8080%2F HTTP/1.1" 302 -
INFO:oauth2client.client:Successfully retrieved access token
INFO:werkzeug:127.0.0.1 - - [10/Apr/2016 15:25:27] "GET /oauth2callback?state=%7B%22csrf_token%22:+%22aa7d28d2496c0e0714eac20b902c1e4db21677d333a837d743525a9696d6c976%22,+%22return_url%22:+%22http://127.0.0.1:8080/%22%7D&code=4/Kz1xkENjHrTzLphfEG8CGND7tkeGXxjIffZxMwSr_hU HTTP/1.1" 302 -
INFO:werkzeug:127.0.0.1 - - [10/Apr/2016 15:25:27] "GET / HTTP/1.1" 302 -
INFO:werkzeug:127.0.0.1 - - [10/Apr/2016 15:25:27] "GET /oauth2authorize?scopes=profile&scopes=email&return_url=http%3A%2F%2F127.0.0.1%3A8080%2F HTTP/1.1" 302 -
INFO:oauth2client.client:Received token response with no refresh_token. Consider reauthenticating with approval_prompt='force'.
INFO:oauth2client.client:Successfully retrieved access token
INFO:werkzeug:127.0.0.1 - - [10/Apr/2016 15:25:28] "GET /oauth2callback?state=%7B%22csrf_token%22:+%22862efe9c1803e12c5c1323b5f68d8bbf185ba89d5d46268924ac527d3dca886c%22,+%22return_url%22:+%22http://127.0.0.1:8080/%22%7D&code=4/nU8o3y3zslgd9KVXJ_NDtzJUZfABeF6ka4IbYSiGKcs HTTP/1.1" 302 -
INFO:werkzeug:127.0.0.1 - - [10/Apr/2016 15:25:28] "GET / HTTP/1.1" 302 -
Eventually I see a page with the words. Invalid request state
This can happen when the cookie session grows to large and the new CSRF token can't be saved to the session. Can you check the cookie sizes in your responses?
We fixed part of this problem with oauth2client 2.0.2, but if you're still experiencing it with that version I'd like to get some more info.
We do highly recommend using a database-backed session such as memcache/redis instead of cookies.

Categories

Resources