开发者

Running a .py file in browser

I want to run python file in browser. I have installed apache. and configured httd.conf file. I have created test.pyfile. I then tried running test.py with my browser by typing htt://localhost/test.py. When I do this, I get the following error:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator开发者_StackOverflow中文版, lohith.pinto@primefocusworld.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.

In my error log

[Thu Jul 07 18:39:55 2011] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified.  : couldn't create child process: 720002: test.py
[Thu Jul 07 18:39:55 2011] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified.  : couldn't spawn child process: C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/test.py

What could be the problem here?


It may also happen if your shebang is not correct (for example, an Unix one like #!/usr/bin/env python). If this is the case, change it to the correct path to your Python executable (for example: #!C:\Python26\python.exe) or look at this answer: How do I ignore the Perl shebang on Windows with Apache 2?


Options +ExecCgi under htdocs, ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

It's what I have and it works


If it's a CGI program, make sure it conforms to the CGI specification (i.e. it outputs a header line with the MIME type and a blank line before trying to write anything else)


Running Python as CGI gateway is easy in windows with Apache. Its similar in windows and Linux if you follow and understand the basics. I will repeat here steps for windows XAMPP installation.

Requirement : you must have XAMPP (not compulsory but need Apache, i work on PHP often hence i've XAMPP)

1) Open Apache config file httpd.conf from Apache folder. 3) Search for "Options Indexes FollowSymLinks" and add ExecCGI option to it. It can look like :

Options Indexes FollowSymLinks Includes ExecCGI

Your might be having less or more options.

4) Search for line "AddHandler cgi-script .cgi", if it has # (comment) before it then uncomment it and add .py handler to it. It should look like:

AddHandler cgi-script .cgi .pl .asp .py

5) Save httpd.conf and restart Apache. 6) Now most imp part is the way you create your py script file. see the example below.

#!C:\Python\Python37\python.exe
print("Content-type: text/html")
print("")
print("<html><head>")
print("")
print("</head><body>")
print("Hello.")
print("</body></html>")

first line must point towards python installation specific to your machine.

#!C:\Python\Python37\python.exe

for linux this can be

#!/usr/bin/python

Also your python script must be placed in htdocs folder or webroot folder for your Apache I was having python 3.7 and hence I added its location as you can see above. Lastly make sure your python code should be specific to version of python you are using. For example in my code earlier i used print "Hello" which was throwing error probably due to python version 3.7. hence i changed my entire code to properly format as per python 3.7 changed it to print("hello")

I hope this helps.


If you haven't done this already, you need to set the proper permissions on test.py to 755.


Is the file in C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/ as the error message states. If not you need to put it there or change your configuration so that it looks in the right location.

Also, as other answers, check that it has the correct permissions and check that it outputs all the necessary headers.


I would change your url to a more likely one in the first instance, ie http://localhost/test.py rather than the one you quoted (htt://localhost/test.py)


you can try mod_python,it supports web applications written in python.the parser gets embedded in the server as an apache module so app runs faster than traditional cgi. Also if you want to stick to cgi way,confirm that all the .py files are in folder named "cgi-bin" and have executable rights.


Using the Window "\" in the first line fixed my problem. In an other thread the right slash "/" was specifically required.

#!C:\Python\Python37\python.exe


Firstly check browser url like that

file:///C:/xampp/htdocs/cgi_python/form.html

That is mean you are code is correct and no error

But output show as a python script code because "code work in consoles not in browser" i.e mean you are directly open a file in htdocs folder and click filename.html and execute then output from in text format For open a browser Solution is

  1. open browser type in url
  2. localhost/filepath

eg. http://localhost/cgi_python/form.html

Then get the answer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜