Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask Socket Error (new to Linux environment)

This is likely a quick fix but I have run into a standstill and I hope you can help. Please bear with me, i'm not fluent in the command line environment.

I'm just beginning with using the Python framework named Flask. It has been successfully installed and I got up and running Hello World. The console was sending me logs while I was calling the program in the browser.

To quit out of the console logs, I pressed ctrl-z (^Z) ~~probably where the error starts?~~ and was prompted with:

[1]+  Stopped                 python hello.py

Now when I either a) attempt to run the program in the browser or b) run the script in command line python hello.py im thrown an error:

socket.error: [Errno 48] Address already in use

..and of course many other lines printed to the console.

A good answer should include what I did wrong and what I can do to fix it, and an accepted answer will also include why ;)

like image 585
achi Avatar asked Apr 22 '26 14:04

achi


2 Answers

You guessed right, the Ctrl-Z is what got you in trouble. Your problem is that Ctrl-Z in effect leaves the application paused, rather than terminated. To terminate the program, you want Ctrl-C.

Your program is using the socket it is configured to use. Attempting to restart the program results in a new Python instance trying to use the socket you have configured the program to use - which is being held by the stopped program.

You have a some options forward from here:

  • In the shell with the stopped Python instance, you could type %1 or fg 1 to go back to running the Python instance you stopped, and having that be what's displaying to your terminal.
    • After doing the above, you could type Ctrl-C, and end the Python instance you have running, making the socket available for a new Python instance.
  • In that same shell, you could type bg 1, which would cause that Python instance to run in the background, not displaying to the terminal. The app should then become responsive. At any point, you could type fg 1 into that command line to get it to display to the terminal again.

There are other options available, including using ps to find the process ID of your Python instance, and then using kill to send signals to that process, if you can't find the command line it's running from.

The manual pages for the shell should give you more help on job control. You can use the man command to read the manual. Type man bash to read the bash manual. If you are running on some other shell, you can just call man with that shell's name.

like image 85
pcurry Avatar answered Apr 24 '26 03:04

pcurry


What you did when you hit CTRL+Z is that you stopped your program and stuck in the background.

It is disconnected from your terminal. Now if you were to type fg 1, you'd get it back. In the meantime, the program is sitting in memory, with all its IO and such tied up. Thus you can't start the program again. But because it's stopped and not running through the processor, you can't use the web part either. If you want to avoid the terminal output, either redirect to a file (python hello.py > hello.log) or to /dev/null if you don't want to ever see the output (python hello.py > /dev/null).

like image 37
pydsigner Avatar answered Apr 24 '26 02:04

pydsigner