How to create a startup service of django for ubuntu server? - python

I am creating a project where I am using an ubuntu server for production.
I need to run the server in the background even though I log out. So, as a solution I am using screen but however if I restart it won't work as we all know and I have to rewrite the screen commands.
So, I want to use the startup service but somehow I am lost in that as I have no idea how to do that.here is the one way to write the script and I love it but how to add the command
python manage.py runserver [IP Address]
in this service.
Sorry if I sound so silly but I need a quick and useful solution, so I thought to be here.
Thanks if anyone can guide me on it.

Related

Setup for running simple python scripts on (Nginx) web server?

I would like to run a couple of very simple Python 3 scripts on the web. As an example, say, the script just reads certain parameters from the URL, sends an email and prints out a simple HTML page saying "success".
I have a virtual private server with Nginx, so I am free to setup the best framework.
My question is: what is the state-of-the-art setup to do this?
More particularly: what do I need to install on my server for Nginx and what do I use in my Python scripts for e.g. reading the URL content? My idea is, that once the server setup is done, I can just put any new script_xy.py file into some directory and it can be accessed using the URL of the script, without a full blown deployment for each script.
Flask If I were to use Flask (or Django), each new script would need its own, continuously running process and its own Nginx setup. That seems like a total overkill to me. Another alternative is web2py, is it the same here or would that be an idea?
CGI 20 years ago I used to program such simple things as Perl scripts using CGI. I read this can be done in principle with Python, but CGI is slow. Then there is Fast CGI. However, my impression was that this is still a bit outdated?
WSGI WSGI seems to be the state-of-the-art alternative to CGI for Python. What python modules would I need to import in my script and what would be the setup for Nginx?
Something else? As you see, I might just need a few hints on what to search for. I am not even sure if I need to search for "Python web framework" or "Python server" etc.
Any help is greatly appreciated.
Thanks a lot for your ideas!
juxeku

Django - Questions around how to validate current environment and how to run in production

my apologies, for what might be a rather simple and amateur set of questions.
I recently was voluntold (more like told) to absorb a django/python project that was developed by someone else. That someone else has left, and now I have to pick this up and run with it as there is no other backup.
Being a university student (intern at work for another 12 mos), I don't think this experience will go to waste AT ALL so I'm happy to be involved, but because this is production and in use today I need to ramp up rather quickly.
// sordid_tale
I am new to Python, Django, Anaconda, PostGreSQL and the world. I know some coding from my first two years, and I've been reading through books to get up to speed.
Current scenario:
Project deployed in production server (windows 2012)
project has an open connection to postgresql and publishes a website used by multiple people across the firm to upload math analytics for reporting.
i started the project up using the python manage.py runserver 123.123.22.22:8000 which started the DEVELOPMENT server and sent me a big ol' warning sign saying don't do this in production.
so i took it down lol. Now trying to figure out how to run in production.
looks like gunicorn and uwsgi are two ways to start the production environment once deployment is complete.
but then I searched on the server folders for gunicorn, uwsgi but got no returns. Running gunicorn and uwsgi returns command is not recognized.
I cannot find any *.wsgi files within the server
Also noted there might be an APACHE dependency, but I couldn't find any apache files in program files.
Q) With above info - is it fair to say the developer never really "promoted" the code to production and was just using the runserver command to get things going on prod server?
Q) Users on their own desktops try to browse to 123.123.22.22 to access site - but they get an error. But browsing to 123.123.22.22:8000 gets them to the site. I read that for HTTP, you need to have a port 80 defined when running server. So python manage.py runserver 123.123.22.22:80. But this gives me an error I don't have privs to run this. To rectify I opened command prompt in Admin mode, but still got the same error. Any assistance?
Once again sincere apologies if the questions are too basic. I am trying to learn a new coding language, but also having to manage the existing prod environment for business continuity, while also trying to get Python, Anaconda, PostGres setup on my local machine for testing and training.
Many thanks!

Can't run coursebuilder in google app engine

It is really weird that after clicking run button, it does nothing and also no log and show a clock sign on the first column.
It works normally before. However, after I messed up my python environment, the google coursebuilder can't run web application. That's my guessing. When I run which python.it only shows:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
These let me feel like I have no way to solve it!Are there anyone who came across this problem before? Any ideas or suggestions?
Updated: I follow suggestions to use command line to run web application on GAE. It reminds me here:
Update: The error message shows that GAE can't get the allocated port and domain. The reason why it happens is that when I use command line to run the web application, I also open GAE GUI to run a web app with the same port number.
So the way to solve it is to close the GAE GUI and free the port. Or we also could designate another kind of port number with command line.(--port=XXXX and --admin_port=YYYY). Or take a look at the doc:
Again thanks for the help of Mihail R!
The OP had multiple issues with GAE setup which were resolved by simply reinstalling the GAE Launcher and making sure the app was first copied into Applications from the .dmg file, then ran from the Applications instead of from inside the .dmg file, and appropriate permissions were suppose to be given so that GAE Launcher created the symlinks it needed to work properly.
More instructions on proper GAE SDK installation can be found here: https://cloud.google.com/appengine/downloads after clicking on the needed SDK and then the OS the SDK will be installed on.

SSH tunnel and Python 3.3

i need to create an application (python 3.3 strictly) where users will save/load their settings online to a remote hosted database. I do not wish to change the database from listening to any other thing than localhost for security reasons, so i assume the best solution for me would be to make the program create some ssh tunnels before the saving/loading happens.
Would this policy make my database unsecure?
How could i make this work? I tried installing paramiko but it is not python 3+ ready.
I also thought maybe i could include into the application's installation, putty tray too and some proper scripting so that i can create that tunnel, but im looking for something clever and efficient here. Ofcourse i would really really really prefer avoiding any extra tray icons or shells from appearing every time a tunnel would activate.
Im asking here so that i can hear an opinion from someone with expirience. As im lacking of that :) . What would be your suggestion?
thx in advance
I am dealing with similar problems at the moment (trying to use Docker's remote API which at the moment doesn't offer authentication). I am currently using bgtunnel, though I'm getting some errors now and then (usually resolved by a webpage refresh) - at first glance due to trying to establish a connection when one already exists. This can probably be solved using some thread.isAlive() stuff, but all in all it is kind of messy managing the connection - is it already alive? Should I check before any request or try and establish it and redo the API call on an exception. It's a bit tricky. I'm also starting to explore Paramiko, hoping it might be a bit easier to handle from Python - the process of porting to Python 3 seems is almost finished at the moment.
That said, I'd expect any decent database to have good-enough baked-in authentication and encryption that I shouldn't be afraid to use it remotely without any ssh tunnelling. It appears from this SO answer that Postrges for example can be configured this way. This means you can kick out ssh and that's one thing in the stack less to think of. They are working on implementing remote auth in Docker's remote API too, at which point I'll gladly do the same :)

How to execute system commands with a button press on a HTML file?

I have a website on my local server and I like to execute system commands on my local server with a button press in the html file that is displayed. Is there a way to either execute system commands like gpio write 0 1 or to run python scripts? And how can I get the output of a system command as a string, like /opt/vc/bin/vcgencmd measure_temp| egrep "[0-9.]{4,}" -o output e.g. 44.4?
Thanks
David
You will have to a webserver with some kind of server-side script. There's lots of ways you can do this. If you know PHP, that may be easiest. If you want to use python check out uwsgi.
Here is a pretty simple project I wrote with uwsgi that might help you get started if you go that route. I found a lot of the examples didn't help a lot, so you might have some luck with that code.
Edit: Actually, uwsgi on the pi is a pretty old version, and it does some weird things on ARM if you try to compile it.
I created a proof of concept for you here using gunicorn instead. Just follow the instructions under the Installing section.

Categories