I'm trying to use Python in my project (without any other framework as requested by my teacher.)
So I've installed Apache 2.x and mod_wsgi and, following a tutorial, I've run my first Python script.
In the specific, the tutorial told me to write in the file /etc/apache2/conf-available/wsgi.conf the following line:
WSGISriptAlias /test /var/www/html/test.py
...and it works!
Despite of this, if I try to change the containing folder of the Python source, it won't work anymore. Can someone tell me how to make this work and what is the meaning of the line written above?
P.S. I need the Python script because my site, in relationship to a form input, have to decide which page to open next.
So I have something like:
...
<form action="test" method="GET" name="query">
...
</form>
I've omitted the .py extension because tutorial said it.
Thanks to all!
As per the documentation:
Description: Maps a URL to a filesystem location and designates the target as a WSGI script.
Syntax: WSGIScriptAlias URL-path file-path|directory-path
Context: server config, virtual host
So when someone hits the url /test on your site, the module at the filesystem path you specify gets run.
Related
I am working on a Django based application whose location on my disk is home/user/Documents/project/application. Now this application takes in some values from the user and writes them into a file located in a folder which is under the project directory i.e home/user/Documents/project/folder/file. While running the development server using the command python manage.py runserver everything worked fine, however after deployment the application/views.py which accesses the file via open('folder/path','w') is not able to access it anymore, because by default it looks in var/www folder when deployed via apache2 server using mod_wsgi.
Now, I am not putting the folder into /var/www because it is not a good practise to put any python code there as it might become readable clients which is a major security threat. Please let me know, how can I point the deployed application to read and write to correct file.
The real solution is to install your data files in /srv/data/myapp or some such so that you can give the webserver user correct permissions to only those directories. Whether you choose to put your code in /var/www or not, is a separate question, but I would suggest putting at least your wsgi file there (and, of course, specifying your <DocumentRoot..> correctly.
I am a newbie to web development and Python. Since I dont have the vocabulary to ask the exact question, here is a summary of what need to do:
I have a small test python cgi script, which i have uploaded to /home/username/pyscripts which is above the /home/username/domain.com
I need a link I can type in the URL bar, which will lead to the script being executed and the content displayed in the browser.
Can someone tell me If i need to create an html file, and if yes how to get it to point to the python script The domain folder has wordpress installed. My hosting is dreamhost shared hosting
The script is there below:
#! /usr/bin/python
print 'Content-type: text/html'
print ''
print 'Hello, World!
Heroku is a good place to host and python scripts.
Pre-req
pythonscripts.py
procfile
requirements.txt
and After add, commit and push the scripts to heroku app. Just run the following command on terminal to run the scripts.
heroku run python your_scripts.py
More if you want to run this scripts on a schedule timing. then heroku provides lots of adds-on. just search it on heroku
Usually you'd need to put your python script under the /home/username/bin/ folder. I'm not sure if your particular webhost actually allows you to run your Python script outside of the /bin folder (normally this is not the case), but if yes then you can substitute the /pyscripts folder.
The URL would look something like this: www.domain.com/bin/mypythonscript.py
Or with the pyscripts folder (if possible with your webhost): www.domain.com/pyscripts/mypythonscript.py
You don't need to create an HTML file as the first content line that you print in your Python script is telling the user's browser to display the output of the script like an HTML file. You simply type the URL to your python script into your browser and then the server runs the script and outputs it as a text/HTML file, which your browser then reads and displays.
Also, don't forget - you need to grant execute/read/write permission to your Python script file after you upload it to the correct folder on your webhost server or it won't run at all. Usually this is done through your upload utility like Filezilla or using a shell command like chmod.
Well dream host support python. Check if they are providing shell access deployment. All you need is create .py file and run it.
Then consider to use Django or Jinja2 like framwork. Its easy for creating web application
I have experience with PHP but now have to manage a python application. It is already developed and running on live server. On live server we have beta.domain.com as well. which has its own copy of same source, other than the main domain application.
Now, when we print something within template it effects but before that, following MVC, if we try to print or use sys.exit() in manage.py or later imported "settings.py" or then views.py, nothing effects these files at all. After any change in just mentioned files, Website still renders everything and display related template.
Another thing that if even we remove
return render_to_response('home.html', RequestContext(request, context))
inside the views.py for testing, nothing effects and website still gets rendered with template.
Do I need to think that if website code is already hosted then in order to have changes to reflect new one in the code in any file (.py), needs to be reinitialized by executing any project related file?
I am not sure how python code needs to be updated at all, any quick help is much appreciated.
Anytime you change your code, you need to restart Apache server.
<path to apache>/bin/apachectl restart
The python interpreter of the process has already loaded your python modules in previous web requests. And once the module is loaded, it is stored in memory. Next time when a request comes, the Python interpreter will simply use the version of the module that is already loaded in memory. So your changed code will not be picked up.
Please, please, please don't try and edit files on your live server. You're only going to get yourself into terrible difficulties. Use a local copy for development, preferably cloned via a version control system, and deploy at regular intervals rather than on every change.
To answer your question, you usually need to restart the WSGI process in order to see Python code changes. An easy way to do this is to touch the .wsgi file, at which point mod_wsgi will detect that it has changed, and reload everything. Otherwise you can simply reload Apache.
Unfortunately I don't understand your references to manage.py or sys.exit.
The solution is excatly restarting the apache server as answered by Sudipta .Each time the py files are interpretted .pvc files are created for each corresponding .py files.When the Apache server is restarted all the python source files are interprretted again and new .pvc files are generated and only these files will run till the apache restarts again.
I have a server setup, and working. Trying to get add python scripting support. I seem to have it working, but how do I configure Apache2 to look for index.psp with out me needing to be specific. Meaning, I can specify index.psp, and it works, but I want apache to search for index.html, then index.php, and then also look for index.psp. How do I include .psp in the search?
Set DirectoryIndex index.html index.psp index.psp (docs).
I'm trying to run a site with Django on an IIS-based server. I followed all the instructions on the main site (http://code.djangoproject.com/wiki/DjangoOnWindowsWithIISAndSQLServer), and double checked it with a very good article (http://www.messwithsilverlight.com/2009/11/django-on-windows-server-2003-and-iis6/).
I successfully got as far as setting up IIS to read .py files. Following the main instructions, I can get the server to render Info.py. However, I can't seem to get IIS and Django to play nice. If, for instance, my Virtual directory is "abc", then if I go to "localhost/abc/", the browser simply shows me the content directory for that folder. Furthermore, if I have my urls set up so that "/dashboard/1" should bring me to a certain page, entering "localhost/abc/dashboard/1" gives me a "page cannot be displayed" error.
I'm fairly certain IIS simply isn't referencing or interacting with Django at all. Does anyone have any ideas how to fix this?
Thanks
Here are the original instructions I followed,
basics instructions: https://code.djangoproject.com/wiki/DjangoOnWindowsWithIISAndSQLServer
additional tips: http://whelkaholism.blogspot.ca/
The first thing you should do is install Python 2.5 or 2.6, for 2.7 you need to recompile PyISAPIe, which I have not done. http://www.python.org/ftp/python/2.6/python-2.6.msi
You need to install the version of PyISAPIe that will match your Python Interpreter version, if they do not match, it will fail. Get it there : http://sourceforge.net/projects/pyisapie/files/pyisapie/
Move the extracted folder from the last step at a decent location (i.e. C:)
You need to change the security settings of the PyISAPIe.dll, they suggest Network Service read, but I set everyone, to be sure there are no problems with this
You then have to CUT AND PASTE (Important) the Http folder of PyISAPIe to Lib\Site-Packages of your Python installation directory
Next, you setup IIS (open the manager with inetmgr in run (winkey+r):
Add a new virtual directory and allow executing ISAPI extensions when prompted by the wizard
Add a new wildcard extension in the property of your virtual directory, untick file exist setting
Add Web Service Extension to IIS Manager pointing to the dll, ensure it is allowed
From the PyISAPIe folder, copy examples\django\Isapi.py and paste it in Lib\Site-Packages\Http
In Isapi.py, set the path (i.e. c:\inetpub\wwwroot\ web_site\ django_project ) and DJANGO_SETTINGS_MODULE (i.e. django_app .settings)
When any change is done to your files, use iisreset in your command prompt to apply the changes
Here are some other things you might do
Ensure the path of your db file (if sqlite used) is okay
Do the same with template location settings
In your urls and html files, ensure the path start with the name you gave to your virtual directory alias (i.e. web_site in our example)
Finally, you may encounter difficulties with serving your CSS. If you have any troubles, tell me and I will update my post.
Serving Django with any webserver basically involves three key details:
Telling the webserver, "I want you
to serve content that is provided by
this module that invokes python"
Telling the python module, "I want you to execute python code
using the details in this file"
Telling the file, "I want you to use Django"
If you're getting a directory listing back for your Virtual Directory then it would seem that you should investigate the VD settings to make sure PyISAPIe is configured for that directory (key details #1).
From the article you mentioned:
Open the IIS Management Console, and create a new virtual directory, and
allow executing ISAPI extensions when
prompted by the wizard.
View the properties of the new folder and click on the
"configuration" button (if it's greyed
out, click 'create' first), then add a
new wildcard extension (the lower
box), locate the pyisapie.dll file and
untick the "check that file exists"
box.
In the IIS Manager, go to the "Web Service Extensions" section, and
right click -> add new web service
extension.
Give it a name (it doesn't matter what), add the pyisapie.dll
fill as a required file and check the
box to set the extension status to
allowed.