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.
Related
This question already has answers here:
Ruby - net/http - following redirects
(6 answers)
Closed 17 days ago.
I'm currently simply trying to get a simple GET request working in Ruby, however, I'm seeing some strange behavior.
I have an Open Web Analytics application running with Docker and it is reachable at http://127.0.0.1:8080/.
I can reach the login site and everything works fine.
Now I want to do a GET request with Ruby to analyze the body of that request but I cannot get it to work, in other languages like Python or simple GET requests over the terminal it works fine. Why not with Ruby?
Here is my very basic Ruby code:
require 'net/http'
url = 'http://127.0.0.1:8080/'
uri = URI(url)
session = Net::HTTP.new(uri.host, uri.port)
response = session.get(uri.request_uri)
puts response.body
Which doesn't output anything. If I look into the NGINX logs from the container, I can see the request being made but there is no further redirection as with the other methods (see below).
172.23.0.1 - - [02/Feb/2023:20:02:59 +0000] "GET / HTTP/1.1" 302 5 "-" "Ruby" "-" 0.088 0.088 . -
If I do a simple GET over the terminal, it works:
GET http://127.0.0.1:8080/
will output the correct body, and in the NGINX logs I can see the following:
172.23.0.1 - - [02/Feb/2023:20:20:10 +0000] "GET / HTTP/1.1" 302 5 "-" "lwp-request/6.61 libwww-perl/6.61" "-" 0.086 0.088 . -
172.23.0.1 - - [02/Feb/2023:20:20:10 +0000] "GET /index.php?owa_do=base.loginForm&owa_go=http%3A%2F%2F127.0.0.1%3A8080%2F& HTTP/1.1" 200 3200 "-" "lwp-request/6.61 libwww-perl/6.61" "-" 0.086 0.088 . -
Doing it in Python with the following basic code also works and gives similar results as with the terminal GET version:
import requests
x = requests.get("http://127.0.0.1:8080/")
print(x.content)
What am I doing wrong?
Got it working with following redirects (see here):
begin
response = Net::HTTP.get_response(URI.parse(url))
url = response['location']
end while response.is_a?(Net::HTTPRedirection)
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
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!
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.
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)