[Errno 98] Address already in use? : Forums : PythonAnywhere (2024)

OSError: [Errno 98] Address already in use?

Hello, I am using Flask to have a bot communicate between Messenger on a Facebook page using Webhook. my Python code works fine from my computer while local hosting, but I would like to run my code from pythonanywhere instead of locally on my computer.

I changed app.run() to app.run(host='0.0.0.0') (as I was told that this opens up any other ports to run on a server). Won't I need to specify where my code sits, and how do I do this? eg. https://www.pythonanywhere.com/user/EnsurdFrndship/files/home/EnsurdFrndship/mysite/app.py.

'0.0.0.0' doesn't seem to be 'specific'. app.run(host='https://www.pythonanywhere.com/user/EnsurdFrndship/files/home/EnsurdFrndship/mysite/app.py') gives me an error. Also, running my code from pythonanywhere gives me THIS error...

OSError: [Errno 98] Address already in use

What does this mean? Thank you.

EnsurdFrndship | 25 posts | May 3, 2020, 3:34 p.m. | permalink

You cannot run the Flask development server in a console. We have web apps for running your Flask code and making it available to the internet. Use a web app.

[Errno 98] Address already in use? : Forums : PythonAnywhere (2) glenn | 9160 posts | PythonAnywhere staff | May 3, 2020, 4:53 p.m. | permalink

I am using web app now (flask_app.py in my 'mysite' directory). I went to the Facebook Developer App Dashboard (to run my bot on Facebook Messenger from a Facebook page), so I needed to set my Webhook Callback URL and Verify Token. I changed my Callback URL from 'http://<ngrok-hex-address>.ngrok.io/' (my local hosting address) to 'http://ensurdfrndship.pythonanywhere.com/' (my site's address), and noticed I didn't need a Verify Token for it. Usually, Facebook says the Callback URL or Verify Token is not correct. I have my pythonanywhere site running, but when I enter a post in Facebook Messenger from my Facebook page, nothing happens (when I used to get a response from my bot when 'local hosting' it).

EnsurdFrndship | 25 posts | May 4, 2020, 3:36 p.m. | permalink

Look in you access log to make sure that there is a request being sent to your web app. If there is and you are not returning a 200 response to it, look in your error and server logs for tracebacks that you can use to start debugging.

[Errno 98] Address already in use? : Forums : PythonAnywhere (3) glenn | 9160 posts | PythonAnywhere staff | May 4, 2020, 4:17 p.m. | permalink

Thank you. How do I access my log and server logs?

EnsurdFrndship | 25 posts | May 5, 2020, 4:19 a.m. | permalink

if you go to your webapp tab, there will be a section with a link to the logs

[Errno 98] Address already in use? : Forums : PythonAnywhere (4) conrad | 4232 posts | PythonAnywhere staff | May 5, 2020, 9:15 a.m. | permalink

I don't see anything in my error.log.

EnsurdFrndship | 25 posts | May 5, 2020, 4:19 p.m. | permalink

In my access.log, I see...

66.220.149.26 - - [05/May/2020:16:28:53 +0000] "POST / HTTP/1.1" 500 290 "-" "facebookexternalua" "66.220.149.26" response-time=0.002

every time I post something in Facebook Messenger now!... so I KNOW something is going on. :) I just don't know what this means.

EnsurdFrndship | 25 posts | May 5, 2020, 4:32 p.m. | permalink

Every time I post a message in Facebook Messenger from my Facebook page, I see this extra info show up in my access.log...

66.220.149.26 - - [05/May/2020:16:28:53 +0000] "POST / HTTP/1.1" 500 290 "-" "facebookexternalua" "66.220.149.26" response-time=0.002

… but I don't see the message I posted located anywhere in the log, or other information (such as the id of who made the post).

EnsurdFrndship | 25 posts | May 5, 2020, 5:21 p.m. | permalink

If I use the print statement in my Flask program, how do I see what is printed?

EnsurdFrndship | 25 posts | May 5, 2020, 5:42 p.m. | permalink

The "500" in that log line (just after the "POST / HTTP/1.1") means that your site is returning a 500 page -- an internal server error. If you check the error log, you'll see errors appearing there right now (though I agree that it looks like there were not any when you looked earlier).

The most recent error message will be at the bottom of the file, so if you fix that then things should start working.

[Errno 98] Address already in use? : Forums : PythonAnywhere (5) giles | 11424 posts | PythonAnywhere staff | May 5, 2020, 5:47 p.m. | permalink

Regarding print statements, if you use them with the flush kwarg they will appear in the site's server log. For example:

print(f"{datetime.now()} doing such-and-such", flush=True)

...will put a timestamped log line in the server log to help you debug. Don't forget to add a from datetime import datetime to the top of your file.

[Errno 98] Address already in use? : Forums : PythonAnywhere (6) giles | 11424 posts | PythonAnywhere staff | May 5, 2020, 5:49 p.m. | permalink

Also, what if I cause an error to happen in Flask where the error 'loops'? How do I stop my code when that happens so I can re run the code with a fresh start?

EnsurdFrndship | 25 posts | May 5, 2020, 6:09 p.m. | permalink

I got 1 response from my bot! I added...

import sys...print(message['message'].get('text'), file=sys.stderr)

and it prints my message in my error.log

… so it works... but it only worked 1 time. I posted another message in Facebook Messenger, and it showed up in my error.log, but my bot didn't respond. I wonder why it only worked 1 time.

EnsurdFrndship | 25 posts | May 5, 2020, 6:31 p.m. | permalink

Now, my bot sometimes responds to posts I put in Facebook Messenger, and other times, nothing happens.

EnsurdFrndship | 25 posts | May 5, 2020, 6:34 p.m. | permalink

Everything is working fine now. I still wonder how to stop my code and re-run it in case I make an error where my problem 'loops'. When I was 'localhosting' everything, I would have to stop my program every time there is an error, re-run it, wait about a minute before all the POST requests in the queue finish and re-run my program again (to get a 'clean' start). How can I clear the POST queue every time I re-run my program?

EnsurdFrndship | 25 posts | May 5, 2020, 7:04 p.m. | permalink

If there is an error, that particular request that came in would return an error response. Then your webapp worker will wait for another request and process that. So it should be okay, unless you are doing something really weird with looping.

[Errno 98] Address already in use? : Forums : PythonAnywhere (7) conrad | 4232 posts | PythonAnywhere staff | May 6, 2020, 1:45 a.m. | permalink

I found out my problem with my bot SOMETIMES responding to posts in Messenger, while other times, does nothing, has to do with the 'timing' of when a post is made, since I am dealing with threading. For some reason, my bot ALWAYS responded when I had it local hosted.

I found out that in order to 'fix' the problem, I would have to somehow make a function that 'triggers' ANY TIME my variable, 'response' is NOT an empty string. Then, inside the function, I would have 'response' sent to the send_message() function in Flask so the user receives a response, and then set 'response' to an empty string on the next line of my function. Is there a simple way to do this in Python, where 'response' always gets checked (in the background) WHILE my code is running, and calls a function WHENEVER the variable is not empty?

EnsurdFrndship | 25 posts | May 6, 2020, 2:23 a.m. | permalink

Threads do not work in web apps. If you handle your messages during the reguest when your web app is triggered by a post, then everything should work fine.

[Errno 98] Address already in use? : Forums : PythonAnywhere (8) glenn | 9160 posts | PythonAnywhere staff | May 6, 2020, 10:15 a.m. | permalink

I was able tp fix the problem. Thank you.

EnsurdFrndship | 25 posts | May 6, 2020, 5:29 p.m. | permalink

OK, glad you worked it out!

[Errno 98] Address already in use? : Forums : PythonAnywhere (9) giles | 11424 posts | PythonAnywhere staff | May 6, 2020, 6:24 p.m. | permalink

[Errno 98] Address already in use? : Forums : PythonAnywhere (2024)

References

Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 5439

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.