Ended program (Flask) keeps sending output to STDOUT? - python

I'm going totally crazy here. I'm developing a website using the Python Flask framework and since this afternoon my Linux box is behaving like a mad man. Let me explain.
I've got my Flask website on an Ubuntu 14.04 server. I normally run it using ./run.py file using a virtualenv. Today I installed some packages and finally decided I wanted a clean env again. So I deleted my venv folder and created it again, like a did a million times before:
$ virtualenv venv
$ . venv/bin/activate
(venv) $ pip install -r requirements.txt
# EVERYTHING INSTALLS WITHOUT ERRORS
(venv) $ ./run.py
/home/kramer65/mysite/venv/local/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:100: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
* Running on http://0.0.0.0:5000/
* Restarting with inotify reloader
* Debugger is active!
* Debugger pin code: 219-856-383
/home/kramer65/mysite/venv/local/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:100: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
This is all pretty much as expected. I then proceed to my browser and load my website (also like I did a million times before). The browser loads a very long time, but nothing gets loaded. In the browser console I don't even see any connection opening at all.
I then check the terminal and see nothing happening either. So I stop the dev server using a CTRL+C and get the following output (as expected):
^CException in thread Thread-6:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/home/kramer65/mysite/venv/local/lib/python2.7/site-packages/watchdog/observers/inotify_buffer.py", line 59, in run
inotify_events = self._inotify.read_events()
File "/home/kramer65/mysite/venv/local/lib/python2.7/site-packages/watchdog/observers/inotify_c.py", line 296, in read_events
event_buffer = os.read(self._inotify_fd, event_buffer_size)
KeyboardInterrupt
So I check out any network connections which are open:
$ netstat -tulpn
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 6127/python
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
udp 0 0 0.0.0.0:68 0.0.0.0:* -
udp 0 0 0.0.0.0:24675 0.0.0.0:* -
udp6 0 0 :::22996 :::* -
So I decide to kill the python process on port 5000 using kill 6127. What then happens just stunns me. The command gets "accepted" (I get no error), but then I see the browser suddenly loads the website and the terminal starts outputting logs:
(venv) $ kill 6127
(venv) $ 83.161.103.21 - - [2016-04-13 13:59:44] "GET / HTTP/1.1" 200 7334 0.045281
83.161.103.21 - - [2016-04-13 13:59:44] "GET / HTTP/1.1" 200 7334 0.062855
83.161.103.21 - - [2016-04-13 13:59:44] "GET / HTTP/1.1" 200 7334 0.080915
83.161.103.21 - - [2016-04-13 13:59:44] "GET / HTTP/1.1" 200 7334 0.099523
83.161.103.21 - - [2016-04-13 13:59:44] "GET / HTTP/1.1" 200 7334 0.118382
83.161.103.21 - - [2016-04-13 13:59:45] "GET /bower/bootstrap/dist/css/bootstrap.min.css?version=3.3.5 HTTP/1.1" 304 745 0.006047
83.161.103.21 - - [2016-04-13 13:59:45] "GET /bower/font-awesome/css/font-awesome.min.css?version=1448904482.0 HTTP/1.1" 304 745 0.003684
etc. etc.
And from this I don't understand anything. I get the output even though I thought I stopped the dev-server using CTRL+C and ran two commands in between (netstat -tulpn and kill 6127).
I can then hit enter a couple times, which gets me my start line again, and then run arbitrary commands:
(venv) $
(venv) $
(venv) $ pwd
/home/kramer65/mysite
(venv) $
and when I reload the website in the browser I simply get the output again in the same terminal window. My commands, any output of my commands, and the Flask dev-server logs just all get outputted in the same terminal window.
I tried rebooting the server, completely removing all website code and removing and installing the virtualenv again, and I even created a new user and installed everything in there, but I get this same result over and over again.
The thing is that I don't know WHY I get this result. Is it because of Flask, because of the virtualenv, because of some weird bug with the network, because of bash or even something else?!
And most importantly, I have no idea how I can solve this. Does anybody have any idea? All tips are welcome, because I'm pulling out my hair here..
[EDIT]
I ran $ ps -eaf | grep python, with the following results:
$ ps -eaf | grep python
immotest 1489 1444 0 14:33 pts/1 00:00:00 venv/bin/python ./run.py
immotest 1497 1489 1 14:33 pts/1 00:00:00 /home/kramer65/mysite/venv/bin/python ./run.py
immohie+ 1510 1374 0 14:34 pts/0 00:00:00 grep --color=auto python

I already suspected it had something to do with the Flask dev server which spawns a new process to be able to restart itself upon code changes. I had added watchdog to requirements.txt because I use it to sync code from my laptop to my Linux dev server.
The Flask dev server works with Watchdog, but for some reason on this machine it wasn't. After removing watchdog from the server everything works fine again. I wasn't able to isolate the problem with a minimal Flask application.

Related

How to run Django Web app on port 443 and 80 linux - Ubuntu or Raspberry PI?

I am pretty new to Django. I think I cannot run django application as sudo since all pip related modules are installed for the user and not for the sudo user. So, it's a kind of basic question like how do I run django app that can listen for port 80 as well as port 443.
So, far I have tried following option - i.e pre-routing - NAT
I run my app using the following command -
$python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 13, 2018 - 03:04:41
Django version 2.1.1, using settings 'WebBlogger.settings'
Starting development server at http://127.0.0.1:8000/
Next, here is my iptables settings nothing worked for me though
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
$sudo iptables -t nat --line-numbers -n -L
Chain PREROUTING (policy ACCEPT)
num target prot opt source destination
1 DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 8000
Chain INPUT (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
1 DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
1 MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0
2 RETURN all -- 192.168.122.0/24 224.0.0.0/24
3 RETURN all -- 192.168.122.0/24 255.255.255.255
4 MASQUERADE tcp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
5 MASQUERADE udp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
6 MASQUERADE all -- 192.168.122.0/24 !192.168.122.0/24
Chain DOCKER (2 references)
num target prot opt source destination
1 RETURN all -- 0.0.0.0/0 0.0.0.0/0
I did http://
and I see connection refused.
I have no idea how to debug the NAT stuff whether it is actually hitting NAT or not. How can I debug and also what is the correct solution to it?
You do not talk to a Django app directly. While Django has a simple development server available via the runserver command, it is not meant for anything but development work.
What you want is to setup a WSGI server to run your app and a web server to accept actual user requests and proxy them to the WSGI. The commonly used WSGI servers (don't worry about what WSGI is) are gunicorn and uWSGI. Both can be installed using PIP and you don't have to install them as the same user as your app. Gunicorn is a bit easier to use, so I would recommend that one. The most common web server nowadays is Nginx.
Also, you should pack your application into a virtual environment, so you can pack it together with all the dependencies without relying on a specific system having everything installed.
Here is a somewhat dated guide on how to do this. It should be mostly accurate though and is a good place to start
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04
In ideal world you will need a web serve to talk to your Django.
Web server (port 80/443) -> gunicorn (wigs) -> Django (port 8000)
And if you just want Django development server to run on 80 then try
python manage.py runserver 0.0.0.0:80
And make sure no other process is using port 80.

Can't get even a simple uwsgi test to work

I'm trying to get nginx/django/uwsgi set up, and I'm stuck at the first step. I'm just trying to get uwsgi to serve a test application directly to my browser, as described here as a basic test.
So, I create the test.py file:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
And then I start it up with $ uwsgi --plugins python --http :8001 --wsgi-file test.py
The output appears to be OK. I get the message WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x3a14c8 pid: 8295 (default app). I would expect that I would then be able to aim my browser at 127.0.0.1:8001, and see a hello world message. However, chrome just tells me that the site can't be reached.
I've been trying various permutations of tests from all over the internet for a solid day, but this is the simplest test I've found, and no one mentions what to do if it fails.
For reference, I'm using uwsgi 2.0.15 and python 3.6.1 on Arch.
EDIT: Evidently someone thought my question "did not show any research effort." So, to summarize what I've tried:
I checked that iptables wasn't blocking anything
I tried it on various ports
I tried moving on with the tutorial to see if it works with a real django project
I tried doing this all in a virtualenv
I tried running from a uwsgi ini file instead of the command line
I tried starting over any using various other tutorials.
Read this post and tried the answer
Output of $ curl -v 127.0.0.1:8001:
* Rebuilt URL to: 127.0.0.1:8001/
* Trying 127.0.0.1...
* TCP_NODELAY set
* connect to 127.0.0.1 port 8001 failed: Connection refused
* Failed to connect to 127.0.0.1 port 8001: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 127.0.0.1 port 8001: Connection refused
Please let me know if you can think of anything else.

Getting "502 Bad Gateway" with nginx, uwsgi python-flask on ubuntu 16.04

I am following the this to deploy a flask app(simple hello world) on the Ubuntu 16-04. digital Ocean tutorial
Everything works fine till Testing uWSGI Serving. After that I followed the step as described and when I finally reach the bottom and check server IP address then I got:
502 Bad Gateway
Ok fine. I searched and checked my error log, I got this :-
2017/01/16 05:29:27 [crit] 20714#20714: *2 connect() to unix:/home/sajjan/project/project.sock failed (2: No such file or directory) while connecting to upstream, client: xx.9.xxx.xxx, server: 138.xxx.xx.xxx, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: "xx.xx.xx.xx"
So After taking a error log , I created the file project.sock manually. again Go to server ip address and then same error "502 Bad Gateway"
Again checked the error log and found this
2017/01/16 06:07:11 [crit] 20874#20874: *1 connect() to unix:/home/sajjan/project/project.sock failed (13: Permission denied) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX "
I figured out about the permission issue and change the permission using below command
sudo chmod 666 project.sock
Now I checked the permision( using ls -l filename)
-rw-rw-rw- 1 root root 0 Jan 16 05:31 project.sock
Now I go back to check the server's IP but found the same "502 Bad Gateway".
Again I checked the error log and found this :
017/01/16 06:13:31 [error] 20897#20897: *6 connect() to unix:/home/sajjan/project/project.sock failed (111: Connection refused) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX ", referrer: "http:// XX.XX.XX.XX /"
I googled for above error read a lot in last two days but nothing to seem working for me .
I have check these answers but no help stackanswer-1 stackanswer-2 and along with these I checked all the digital-ocean community thread but nothing seems to work.
I am total begineer to servers and don't know much about ubuntu. If you can help me to find out what wrong am I doing or suggest some better tutorial/ways to deploy my flask application, then I would be greatful.
These are my files
hello.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0')
project.ini
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = /home/sajjan/project/project.sock
chmod-socket = 660
vacuum = true
die-on-term = true
wsgi.py
from hello import app
if __name__ == "__main__":
app.run()
Below is file : /etc/nginx/sites-available/project
server {
listen 80;
server_name 138.197.28.107;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/sajjan/project/project.sock;
}
}
When I run the command :
sudo service uwsgi restart
output:
Failed to restart wsgi.service: Unit wsgi.service not found.
while output of
sudo service nginx status/restart
then this show that nginx is running .
Help me, If anything else that you want to know then let me know.
Thanks
EDIT :
I have created a project.service file and its conetent is :
[Unit]
Description=uWSGI instance to serve project
After=network.target
[Service]
User=sajjan
Group=www-data
WorkingDirectory=/home/sajjan/project
Environment="PATH=/home/sajjan/project/venv/bin"
ExecStart=/home/sajjan/project/venv/bin/uwsgi --ini project.ini
[Install]
WantedBy=multi-user.target
I figured out I have to run below command :
sudo systemctl start project
Output is :
Warning: project.service changed on disk. Run 'systemctl daemon-reload' to reload units.
and when I run
sudo systemcl reload project
then output :
Failed to reload project.service: Job type reload is not applicable for unit project.service.
See system logs and 'systemctl status project.service' for details.
and when I check the "systemctl status project.service"
● project.service - uWSGI instance to serve project
Loaded: loaded (/etc/systemd/system/project.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2017-01-16 17:49:29 UTC; 6min ago
Main PID: 27157 (code=exited, status=203/EXEC)
Jan 16 17:49:29 learningwithpython systemd[1]: Started uWSGI instance to serve project.
Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Main process exited, code=exited, status=203/EXEC
Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Unit entered failed state.
Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Failed with result 'exit-code'.
I had the same problem using the guide. As far as I've read; 502 bad gateway is a symptom of Nginx not being able to properly connect to the uwsgi. Changing the permissions on the socket solved the issue for me.
sudo chmod 777 /home/sajjan/project/project.sock
sudo systemctl restart nginx
777 is offcourse a bit excessive, but its a quick and dirty way to verify if it is in fact a problem with permissions
Nginx has no permission to write to socket. Granting appropriate mode with below command, helped me.
chmod 0755 /to/project
Same problem.
But I give the 666 permission and restart all, and it works.
I think the error log only show the problem's one possible cause. While, the journalctl -u <yourproject>.service command help present another reason.
My error log also tells me that he can't find the "myproject.socket". But .ini has already help us build it. Then I get this error: myproject.service: Failed at step USER spawning ~/bin/uwsgi: No such process
So maybe it is the permission's problem.
Try running myapp/bin/uwsgi --ini myapp.ini to see the actual error thats preventing uwsgi to run.
In my case 5 processes in the .ini configuration file were to much to handle for my cpu this was my error output.
your processes number limit is 3900
your memory page size is 4096 bytes
detected max file descriptor number: 1024
If this is your case lowering the number of processes to 2 in your .ini file might work.
For me this problem was due to the users configuration. Basically, I set up all of my project using the root user. But then I redid everything from scratch using a non-root user with sudo privileges. It worked.
I saw your comment on https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04
try run sudo /etc/init.d/nginx start
Then try open http://server_domain_or_IP
If it works, type which uwsgi to find the right uwsgi path and change "/etc/systemd/system/myproject.service"
change
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/uwsgi --ini myproject.ini
into the real path instead of env path.
I figure out this problem through these command.
As many people already mentioned, the 502 error is about socket permissions (you may see Permission denied errors in /var/log/nginx/error.log). However, it all seems to have to work, since
socket's file ownership is your_user:www-data
socket's file permissions are 660 (which means that you and group can read/write socket)
nginx works with the user www-data:www-data (it's configured in nginx .conf file)
The real problem is that nginx can't connect to the socket located in your home directory!
So the solution is simple - just move the socket file somewhere else. For example, to /tmp/ or /var/www/... folders.
Solution:
Create directories
sudo mkdir /var/www/your_project
sudo chown your_user:www-data /var/www/your_project
Modify your_project.ini
socket = /var/www/your_project/your_project.sock
Modify nginx server block
uwsgi_pass unix:///var/www/your_project/your_project.sock;
Restart uWSGI and nginx
sudo systemctl restart your_project_service
sudo systemctl restart nginx
And it all has to work now.

Apache & mod_wsgi settings for Django Python Project

I am trying to setup my django-python project on Google Compute Engine Debian8 VM. I made few config changes in /etc/apache2/sites-available/000-default.conf & /etc/apache2/sites-available/default-ssl.conf files
Then tried to restart the server. And I got following Errors. Uninstalling & reinstalling the apache2 too is not fixing this error. Any suggestions to fix this issue ?
Command:
sudo service apache2 restart
Error:
Job for apache2.service failed. See 'systemctl status apache2.service' and 'journalctl -xn' for details.
Command:
sudo systemctl status apache2.service -l
Error:
apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2)
Active: active (exited) since Wed 2016-08-31 05:08:43 UTC; 3min 37s ago
Process: 3539 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
Process: 3521 ExecReload=/etc/init.d/apache2 reload (code=exited, status=1/FAILURE)
Process: 3546 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
Unit apache2.service entered failed state.
Starting LSB: Apache2 web server...
Starting web server: apache2(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
.
Started LSB: Apache2 web server.
Thanks,
Have you read the error ? It's clearly stated why it fails:
Cannot load /usr/lib/apache2/modules/mod_wsgi.so into server: /usr/lib/apache2/modules/mod_wsgi.so: cannot open shared object file: No such file or directory
do you have libapache2-mod-wsgi or libapache2-mod-wsgi-python3 installed? if you have access to shell you can install it with
sudo apt-get install libapache2-mod-wsgi
# or for python 3
sudo apt-get install libapache2-mod-wsgi-python3
Ran the Command to see the process running on port 80:
sudo netstat -ltnp | grep ':80'
Output:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24482/nginx -g daem
tcp6 0 0 :::80 :::* LISTEN 24482/nginx -g daem
Running the next two commands then fixed the issue:
sudo kill -9 24482
sudo service apache2 restart
Hope it can be helpful.
Thanks,

Problem running Heroku's Facebook app tutorial with Python

I am testing Heroku's ability to write a Facebook app with Python. I'm having a problem running the basic tutorial. It seemed like this question was worth asking on StackOverflow in case there's an answer that helps other people who run into the exact same problem.
I followed the instructions on heroku's facebook development page (http://devcenter.heroku.com/articles/facebook). Deploying to Heroku worked fine.
However, running the app locally does not. When I follow the instructions and bring up
http://localhost:5000
I get to the Facebook Login screen. But when I click Log In on that screen, I get:
SSL connection error Unable to make a secure connection to the
server. This may be a problem with the server, or it may be requiring
a client authentication certificate that you don't have. Error 107
(net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error.
and the console output is
09:55:07 web.1 | https://localhost:5000/ 09:55:07 web.1 |
https://www.facebook.com/dialog/oauth?client_id=179852202088814&redirect_uri=https://localhost:5000/&scope=user_likes,user_photos,user_photo_video_tags
09:55:07 web.1 | 127.0.0.1 - - [24/Sep/2011 09:55:07] "GET / HTTP/1.1"
302 - 09:59:02 web.1 | 127.0.0.1 - - [24/Sep/2011 09:59:02] code 400,
message Bad request syntax
('\x16\x03\x00\x00U\x01\x00\x00Q\x03\x00N}\xe2&\xf9\xf7"\x15\xd5\xb6\xf6\xa6\x0f\xb01\x97N\xcc\xb3l\xed\x97\xd1!-\x91c?\x1f\xac\xa2h\x00\x00*\x00\xff\x00\x88\x00\x87\x009\x008\x00\x84\x005\x00E\x00D\x00f\x003\x002\x00\x96\x00A\x00\x04\x00\x05\x00/\x00\x16\x00\x13\xfe\xff\x00')
09:59:02 web.1 | 127.0.0.1 - - [24/Sep/2011 09:59:02]
"UQN}?&??"ն??1?N̳l??!-?c???h*???98?5EDf32?A/??" 400 -
When I try it in Safari, the address bar shows the following very long URL:
https://localhost:5000/?code=AQBPWpkbRdL2bt7KER0fcUS9ZnheXiGApkaF5MXbNgyIJqzw46SGve1iVyLIx1sDltNh0PkXPDdxhjAxoa1YED1cpcaflCXCkqzO27A-rhgjBpXwWUClpGRpRmDD2eIXcOyIczo_qGf45tbpvDZO5hFa0gmUeSHri4vY3bqw-5jBjZRoZfEB7pI8cLPOIsnNICI#_=_
Safari compains that it can't establish a secure connection.
This is running on OS X 10.6.8.
This is because https is not enabled locally on your machine, you could enable this or you could alternatively run without the SSL on your localhost. To do this you would edit the function to look something like:
def get_home():
return 'http://' + request.host + '/'

Categories

Resources