-1

I have worked with Python for a while, but this is my first time that I have to install it and use it on a VPS server.

I have the following script hello.py:

#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

print '''
<!DOCTYPE html>
<html>
<head>
   <title>Hello</title>
</head>'''
print "Hello World!"

I haven't been able to run the file on a browser. I don't know if it has something to do with the location of the script or with some other permissions I need to consider. I think there is nothing wrong with the code, because I copy exactly the same in another server (which I didn't configure) it works fine. Also, If I run the script from the console, it also works.

If I run the file on the location project/cgi-bin/hello.py the browsers display "Internal Server Error". In console the error is: No such file or directory: exec of /project/cgi-bin/hello.py.

If I run the file on the location project/hello.py the browser displays plain text.

I've been reading a lot, and I think that maybe the problem has to do with some configuration on de HTTP, or Apache2 directory.

I would greatly appreciate your help.

ruohola
  • 21,987
  • 6
  • 62
  • 97
mauguerra
  • 3,728
  • 5
  • 31
  • 37
  • 1
    I guess you are trying to make a simple web application *accessible* by browser (not running it in a browser). In this case you should have a look at one of the many Python web frameworks. A small one to start with might be [flask](https://flask.palletsprojects.com/en/1.1.x/) or one for larger projects is [Django](https://www.djangoproject.com/). – Klaus D. Jan 18 '21 at 05:44
  • 1
    You are trying to run a Python script as a `CGI` *"Common Gateway Interface"*, which you can do but you must configure your Apache (or other) server to allow it. Example and details here... https://www.tutorialspoint.com/python/python_cgi_programming.htm – Mark Setchell Jan 18 '21 at 12:05

2 Answers2

0

You don't do anything. Python does not and cannot run in a browser.

ruohola
  • 21,987
  • 6
  • 62
  • 97
0

You could try to open a browser with webbrowser. Safe your HTML code in a separated HTML-file. Have a look at the Docs.

alexrogo
  • 522
  • 3
  • 17