Running a python script on my hosting - python

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

Related

beyond Compare not creating html report on Linux (Debian)

I am deploying a Python Flask app on Openshift Platform by creating a Docker File.
Now my app is up and running fine but there is a feature where I have to use Beyond Compare for comparing 2 .txt file and generate a report in .html format.
The app is running fine on a Windows Server and generating all html reports and everything.
Now when I am trying to run the same Python script it is not creating the html report file.
Below is the Python command I am executing.
subprocess.call(["/usr/bin/bcompare", '#"/project/ByndCmpResults/myscript.txt"' ,file1, file2, report_filename], shell=True)
file1, file2 and report_filename are correctly defined.
Below is the content of myscript.txt
options:display-all&
output-to:"%3" &
output-options:wrap-word,html-color "%1" "%2"
Below is the snap when I am running above command.
Requesting you to please help with the issue as I have been stuck with this since last week.
Beyond Compare on Linux requires an X-Window session to run, even when scripted. It will not run as a console application. Removing the X-Window session requirement is on the feature request list for a future version, but there isn't a workaround in the current version.

Python program crashes when run through a Windows service

My client has provided me with a Python console application which performs some work and writes the result into a .txt file. My task is to write a Windows service which reads that particular .txt file and performs further actions.
I used C# on .NET to write the service. My solution contains 3 projects:
The logic layer project.
The Windows service layer project.
A test app layer project, used for debugging and other purposes.
Both the Windows service layer and test app layer are using the logic layer for core functionality. When I run the application through the test layer, everything works perfectly, but whenever I try to run the application through the service, the Python standalone application that the service launches doesn't write any output files. I could see that the Python app runs in the task manager, but there's no output anywhere. I believe the Python code is crashing but I couldn't get the exact reason.
I've tried the following ways to debug the issue:
Searched the Windows and System32 directories for any related output files, just to consider the possibility of the service having these directories as the default working directory.
Used absolute paths in the service code to make sure that the Python part is not writing output files to some unknown location.
Had the client implement passing the output directory to the Python code through command line arguments.
Wrote a mock console app in C# which writes a file, tried to call it through the service, but it worked fine and wrote the file as expected.
Suspected the standard IO could be causing the Python application to crash and thus used the standard IO in my mock program, but it worked without any issues.
Tried giving a long task to the Python code, which should've taken about 30 minutes to execute completely, but the Python script ran and closed immediately, which essentially is reliable proof of the theory that it crashes at some point.
Tried running the service with my unelevated Windows user instead of the Local System pseudouser.
Tried configuring the service to be able to interact with the desktop.
I am all out of ideas here. Any direction I should also search in?
Just a note, if that matters: I am using System.Diagnostics.Process to launch the Python script.
If it works from your test app, it sounds like a permissions issue. What security context / user is the windows service running as, and does that user have permission to write to the filesystem where you are expecting it? Have you tried using a full path to the output file to be sure where it is expected?
I'd be inclined to write a tiny python app that just saves "hello world" to a file, and get that to work from a windows service, then build up from there.
Thanks to the help from timhowarduk, I finally was able to get to the root cause of the problem. The python script was looking for a configuration file, and when it was running from the Windows Service, it was looking for that config file in System32.
All the windows services are run from System32.
The above caused the python script to search in System32 since it was running as part of the windows service. I guess I might just ask the client to edit the python script to read config from the windows service application directory.

Django deployed app/project can't write to a file

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.

Displaying directory content in html page with python

I want to write a program in python which can read the files under a directory and can show them as hyperlink in a html page created by python program dynamically , can anyone please suggest i am newbie for python.
Quick hack using SimpleHTTPServer
Go to the directory, you want to have listing of and run:
with Python 2.7:
$ python -m SimpleHTTPServer
with Python 3.x:
$ python -m http.server
This will start local simple HTTP server on port 8000.
Visit http://localhost:8000 and you will see listing of the current directory, which is even browsable.
Warning: Using this solution can be a security risk, as all the files are accessible for whoever visits published url.

Calling an executable from within Python / Django web application running on IIS

I have a Python / Django application which is supposed to call an external windows binary and get its output at some point. And it does so when tested via 'python manage.py shell'.
But when it is run from within the web browser, which is served by IIS, the external application is not executed.
Is IIS blocking something on the way? Can this be avoided?
Any help is much appreciated.
oMat
Might be a permissions issue. when you run from the shell, you're using the user that run the python manage.py shell command. When serving requests from the IIS you're using its user (IUSR or something like that). Try giving execution permission on the executable file to the Everyone group just to see if it helps.

Categories

Resources