strange behavior with flask on windows - python

I'm maintaining a web application built with Flask with python 2.7 coupled with Jinja and angularjs .In a Linux environment, everything is working fine.
On windows when I run the application on cmd or git bash (python app.py), I only see that the server is running and in which port (and everything else is working fine in the browser), but the problem is that the logs in console aren't shown like in a Linux terminal.
For example, I can't see the requests like: POST /login..or an exceptional mission or even a simple print "test" Dosen"to show (still everything is working in a browser).
Even worst, when I terminate the server with "ctr+c" all the previous messages and logs are printed in the terminal, all together in one single dump!
--- Update ---
when i use the command python -u app.py
it's even worst , the application dose'nt run in the browser anymore , no log in console and when i termiante it shows this :
screenshot of terminal

I would suggest that you creat a proper uWSGI gateway.
An example setup could be:
Linux 18.04 ->
Nginx / Apache reverse web proxy mode ->
Gunicorn (which has a debugger you can attach, this will output to the regular log files like other applications. Systemd logs I think) ->
Flask web framework.
Hope this helps.

Related

How to run Django server constantly on windows

I wrote a code for a Django server, and it works perfectly inside the shell of Pycharm.
Now, I want to run this server on a local computer constantly without being inside Pycharm's shell.
Also, because it's for a client of mine I don't want any open CMD windows or any other weird GUI- I want him to just access the website like any other website.
I've seen all kinds of solutions- running runserver with &, creating a virtual machine and running the server on it and etc.
I am familiar with Vmware and all, so if the proper solution is this It's OK. But I wonder- are there any other ways to run a server on a PC without installing any additional programs?

How to automatically launch django web server by windows SYSTEM

I want to know if there's a way to have windows server 2019 automatically launch django's web server. I also want the launch to be performed at startup and by SYSTEM.
I tried using batch scripts that launch manage.py from venv's python interpreter. When I launch the batch manually (i.e. double click) it works fine and dandy. But it appears that SYSTEM fails in running the script correctly when planning the task.
I made SYSTEM launch another script at startup (a simple python script that creates a txt file from within its own venv) and it works.
If the Django launch sceipt is launched by USER then it works.
The problem is with the launching of django with SYSTEM. I've also tried streamlit and the result is the same.
Do you have any Ideas?
Sample batch script:
cd path\of\managepyfile\
C:\path_to_venv\Scripts\python -m manage.py runserver
We run a similar application (not python) but an application that uses a web server.
We have it setup as a task in task scheduler that when the server starts up, it runs the powershell script that executes a command to start the web server.
Link to setup
However, you could use a web server like IIS and deploy the files to the www folder in the cdrive and run the site as an IIS service.
Setting it up on IIS was a little tricky if you've never used IIS before. Happy to help out as we have deployed our test access tool for one of our apps this way.

Debugging django remotely

I'm developing Django server on Ubuntu OS. Since there is no browser on that machine, I can only debug the server remotely. So I just configure it with Apache and WSGI, and now I can access it through machine public IP.
Then I want to record logs in some views for debugging, if I output the log to a file, I can see it in the file, but if I want to output it to the console, I just get confused here, where is the console? since I didn't launch it with python manage.py runserver manually, currently running server process was launched by WSGI automatically. Of course, I can just stop the process launched by WSGI, and re-launch it with python manage.py runserver manually. If so, I can't access it through machine public IP.
So how can I see logs in the console in putty
Firstly, you shouldn't be developing on the server. Do that locally and debug in the usual way there.
If you're debugging production issues, you will indeed need to use the log files. But it's pretty simple to see those in the console; you can do tail -f /var/log/my_log_file.log and the console will show the log as it is being written.
You cannot output it to the console. Since the process is not called from a console, you cannot see the stdout in a console. You can only redirect the output to a file and read the file.
If at all you want the logs in the console, then you have to call the django server from console. i.e python manage.py runserver, which should only be used for development time, as this server is not good to be used in production

How to debug Django app running on Heroku using a remote pdb connection?

To debug a bug I'm seeing on Heroku but not on my local machine, I'm trying to do step-through debugging.
The typical import pdb; pdb.set_trace() approach doesn't work with Heroku since you don't have access to a console connected to your app, but apparently you can use rpdb, a "remote" version of pdb.
So I've installed rpdb, added import rpdb; rpdb.set_trace() at the appropriate spot. When I make a request that hits the rpdb line, the app hangs as expected and I see the following in my heroku log:
pdb is running on 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc:4444
Ok, so how to connect to the pdb that is running? I've tried heroku run nc 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc 4444 to try to connect to the named host from within heroku's system, but that just immediately exits with status 1 and no error message.
So my specific question is: how do I now connect to this remote pdb?
The general related question is: is this even the right way for this sort of interactive debugging of an app running on Heroku? Is there a better way?
NOTE RE CELERY: Note, I've now also tried a similar approach with Celery, to no avail. The default host celery's rdb (remote pdb wrapper) uses is localhost, which you can't get to when it's Heroku. I've tried using the CELERY_RDB_HOST environment variable to the domain of the website that is being hosted on Heroku, but that gives a "Cannot assign requested address" error. So it's the same basic issue -- how to connect to the remote pdb instance that's running on Heroku?
In answer to your second question, I do it differently depending on the type of error (browser-side, backend, or view). For backend and view testing (unittests), will something like this work for you?
$ heroku run --app=your-app "python manage.py shell --settings=settings.production"
Then debug-away within ipython:
>>> %run -d script_to_run_unittests.py
Even if you aren't running a django app you could just run the debugger as a command line option to ipython so that any python errors will drop you to the debugger:
$ heroku run --app=your-app "ipython --pdb"
Front-end testing is a whole different ballgame where you should look into tools like selenium. I think there's also a "salad" test suite module that makes front end tests easier to write. Writing a test that breaks is the first step in debugging (or so I'm told ;).
If the bug looks simple, you can always do the old "print and run" with something like
import logging
logger = logging.getLogger(__file__)
logger.warn('here be bugs')`
and review your log files with getsentry.com or an equivalent monitoring tool or just:
heroku logs --tail

How can I create an local webserver for my python scripts?

I'm looking to use a local webserver to run a series of python scripts for the user. For various unavoidable reasons, the python script must run locally, not on a server. As a result, I'll be using HTML+browser as the UI, which I'm comfortable with, for the front end.
I've been looking, therefore, for a lightweight web server that can execute python scripts, sitting in the background on a machine, ideally as a Windows service. Security and extensibility are not high priorities as it's all running internally on a small network.
Should I run a native python webserver as a Windows service (in which case, how)? Or is it just as easy to install Apache onto the user's machine and run as CGI? Since this is all local, performance is not an issue either.
Or am I missing something obvious?
Don't waste a lot of time creating Windows service.
Don't waste a lot of time on Windows Apache.
Just make a Python service that responds to HTTP requests.
Look at https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/3/library/http.server.html for version 3
Python offers an HTTP server that you can extend with your server-side methods.
Look at http://docs.python.org/library/wsgiref.html
Python offers a WSGI reference implementation that makes your server easy and standards-compliant.
Also http://fragments.turtlemeat.com/pythonwebserver.php
"I'm trying to avoid making the user run python stuff from the command prompt."
I don't see how clicking a web page is any different from clicking desktop icons.
Starting a web server based on Python is relatively easy, once you have the web server. First, build the server. Later, you can make sure the server starts. Let's look at some ways.
Your user can't use a random browser to open your local page. They need a bookmark to launch "localhost:8000/myspecialserverinsteadofthedestop/" That bookmark can be a .BAT file that (1) runs the server, (2) runs firefox with the proper initial URL.
You can put the server in the user's start-this menu.
You can make your Python program a windows "service".
Best way is to make your own local server by using command prompt.
Make a new folder say Project
Make a new folder inside project & name it as "cgi-bin"(without quotes)
Paste your .py file inside the cgi-bin folder
Open cmd and change to the directory from which you want to run the server and type "python -m CGIHTTPServer"(without quotes)
Minimize the cmd window & open your browser and type "localhost:8000/cgi-bin/yourpythonfilename.py"(without quotes).
The wasiest step would be navigate to folder where your files are located and running http.server module
cd /yourapp
python3 -m http.server
the you should see something like this in console
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Running a native python webserver as a windows service should be a no brainer. Check out the documentation for writing windows services (win32api, ActiveState python) in python and also the documentation for subclassing BaseHttpServer and SimpleHttpServer.
BTW: I had a similar question on stackoverflow: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?
Basically, you subclass BaseHTTPServer (you have to anyway...) and then... but just read the accepted answer - it set me on the right track!

Categories

Resources