Debugging django remotely - python

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

Related

Django project is not running on browser

I am following a basic django project, mainly so I can see it run on the browser, because I have another django project where it says it runs on http//127.0.0.1:8000/ but on my browser it shows errors.
I am working linode in cmd on windows laptop, I have edited my firewall on linode to allow ssh, http and https.
https://docs.djangoproject.com/en/4.0/intro/tutorial01/
this is the tutorial I am following.
This is what my code says in cmd.
This is what my browser says.
The tutorial assumes your test server is running on the same machine; 127.0.0.1 is a shorthand for it. Your server is actually running on linode, so you need two things:
expose the server interface outside, as 127.0.0.1 is only accessible internally. The command is: python manage.py runserver 0.0.0.0:8000, see this question for more details.
access it by the remote machine IP/hostname and expose server on external interface. So, in your browser, instead of 127.0.0.1:8000, use <linode_machine_IP>:8000

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.

How to handle Python files with Apache

So I want to get into React Native development and have decided Python to be my backend, but for some reason I cannot configure the Apache correctly. The only way to successfully get the result from the request is to include path to python.exe at the start of the document like so:
!C:\Users\Name\PycharmProjects\AppName\venv\Scripts\python.exe
But the problem is that the file is than executed by the Python console, and if I want to access it via mobile phone I get this error:
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
So my question is:
Is there any way in which I can configure Apache to execute a file, without the requirement of the py console, so the request might be handled by devices, which doesn't have a Python console installed?
if you connect from mobile device to http://192.168.1.3/HelloWorld.py on server with CGI then code should be executed on server, not on mobile device. If CGI doesn't work then server may try to send code as normal file and then mobile mdevice ay try to run it locally but it is wrong - CGI server should run code on server.
At start I would put code in subfolder cgi-bin to run it as http://192.168.1.3/cgi-bin/HelloWorld.py because most CGI servers as default run code only in this subfolder.
On Linux script would need shebang
#!/usr/bin/env python
in first line and it should be executable
chmod a+x script.py
CGI has also some rules how to generate data which it will send to client. It may need at start extra information for HTTP protocol - and using only print("Hello World") may generate wrong data and it may have problem to send it. You should have it in any tutorial for CGI scripts. See module cgi
To run Python's code Apache needs module mod_cgi, mod_fcgi or mod_python
mod_cgi and mod_fcgi can run scripts in different languages: Python, Perl, Ruby, etc. and even Bash, PHP or C/C++/Java
Python3 has standard module http which can be used also as simple server
python3 -m http.server --cgi
and it will serve all files in folder in which you run it. And it runs files from subfolder cgi-bin/ - see doc: http

strange behavior with flask on windows

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.

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

Categories

Resources