Python3 Cgi Https Server Fails on Unix

Python3 CGI HTTPS server fails on Unix

I found the answer at:

http://www.castro.aus.net/~maurice/OddsAndEnds/blog/files/d2baf24c48b972f18836cac7a27734e2-35.html

The solution is to add:

http.server.CGIHTTPRequestHandler.have_fork=False # Force the use of a subprocess

before starting the server.

This is required for Mac and Unix implementation because, for efficiency reasons, they employ a fork to start the process that executes the CGI rather than creating a subprocess as used by other implementations (i.e. Windows). In a non-wrapped CGI implementation the fork works fine and the output is sent to the socket correctly, however, when the socket is SSL wrapped things go terribly wrong.

The solution is to force the Unix and Mac implementations to use a subprocess leaving the SSL socket happily working and having the Python Server transfer the output of the CGI script to the client while translating the output into SSL.

I still have no clue why this used to work!

Run python cgi script in browser w/o errors

At a guess, you have your script in /var/www/cgi-bin/cgi101.py.

But your Apache configuration has this:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

and add the AddHandler directive in there as well:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler cgi-script .cgi .py
</Directory>

(and perhaps remove the section above this one, for the /var/www directory.)

I would then put your script at /usr/lib/cgi-bin/cgi101.py, and try again.

The Apache tutorial seems actually pretty clear on this.

c++ cgi app calling other program fails

This is likely a permissions issue. By default your CGI application will be run as if by user nobody. The program you want to launch should be executable by "nobody".

How to run CGI hello world with python http.server

From the http.server docs:

CGIHTTPRequestHandler can be enabled in the command line by passing
the --cgi option:

$ python3 -m http.server --bind localhost --cgi 8000

Put your script into cgi_directories:

This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts.

Open in the browser:

$ python -mwebbrowser http://localhost:8000/cgi-bin/hello.py

where hello.py:

#!/usr/bin/env python3
print("Content-Type: text/html\n")
print("<!doctype html><title>Hello</title><h2>hello world</h2>")

I had to make it executable on POSIX: chmod +x cgi-bin/hello.py.

What is the Python 3 equivalent of python -m SimpleHTTPServer

From the docs:

The SimpleHTTPServer module has been merged into http.server in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

So, your command is python -m http.server, or depending on your installation, it can be:

python3 -m http.server

Python 3.4 ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1638)

SSL/TLS is a layer on top of TCP. But, while TCP is done in the kernel SSL/TLS is done in user space. There are two problems because of this:

The first problem is that the user space and thus the SSL state is duplicated when doing the fork inside subprocess.Popen. But any actions on this socket in the parent or the child will not be synchronized with the state in the other process so if both processes actively deal with the socket (which includes closing it) it will cause a failure in the SSL state which results in an error in the connection.

Even if only the child process deals with the problem the exec after the fork (as done in subprocess.Popen) will make only the kernel file descriptor available to the process (rvim in your case`) but not the user space SSL layer, since this user space was inside the Python process only. This way the subprocess will communicate using a plain TCP socket whereas the server expects SSL. This will also cause a connection error and ultimately connection close. See also Python3 CGI HTTPS server fails on Unix.

The only way I could think of is to keep the SSL socket in the parent only and use separate pipes (or socketpair) to communicate with the subprocess. All data received from the server in the parent (using SSL) should be forwarded as plain to the subprocess and all data from the subprocess should be send with SSL to the server inside the parent.

Error 500 on trying to run python scripts on local Apache server

FastCGI is a completely different protocol then CGI. You can't just set fcgid as handler for a CGI script to have it run as FastCGI process, a FastCGI process needs to communicate with the hosting server using different mechanisms then stdin/stdout.

For python there is an implementation of a FastCGI wrapper called flup, which allows to host python WSGI applications on top of FastCGI, which can be useful if a server provides support for FastCGI but not WSGI. The program hasn't seen much activity in a while, but there are forks that provide compatibility for python3, e.g. flup6.

To get started you need to install flup, and make sure the user the script is running as can import it, a good way is to use a venv, for example:

$ python3 -m venv /path/to/venv
$ chmod a+rX -R /path/to/venv
$ /path/to/venv/bin/pip install flup6

The "Hello World" for FastCGI using python3 and flup then looks something like this:

#!/path/to/venv/bin/python3

def wsgi_func(environ, start_response):
start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")])
response = [b"<html>Hello World</html>"]
return response

if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(wsgi_func).run()

The WSGI function must return an iterable of bytes in python3 and not a single bytes or str, so if the response is created as a list of str it needs to be encoded before returning, lots of tutorials still use python2 and examples taken from those then would fail to run on python3.

An alternative to using FastCGI would be to use mod_wsgi which allows to directly host WSGI applications.

run python script as cgi apache server

I think you are missing a print statement after

print("Content-type: text/html")

The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is
following.

The second section is usually HTML, which allows the client software to display nicely formatted text with header, in-line images, etc.

It may look like

#!/usr/bin/env python

print "Content-Type: text/html"
print
print """
<TITLE>CGI script ! Python</TITLE>
<H1>This is my first CGI script</H1>
Hello, world!
"""

For more details visit python-cgi

For python3

#!/usr/bin/env python3

print("Content-Type: text/html")
print()
print ("""
<TITLE>CGI script ! Python</TITLE>
<H1>This is my first CGI script</H1>
Hello, world!
"""
)

Python CGI script doesn't run

For running python script with httpd in theory You need to check that this is exist:

in httpd.conf :

Include conf.modules.d/*.conf
LoadModule wsgi_module modules/mod_wsgi.so
IncludeOptional conf.d/*.conf # -------------------- End of file

Then You can try to create youvirtualhost.conf in /etc/httpd/conf.d/
And add something like this:

<VirtualHost www.youdomainname.com or *:80>

ServerAdmin admin@youdomainname.com
ServerName www.youdomainname.com
ServerAlias www.youdomainname.com

WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup %{GLOBAL}

Alias /wsgi-scripts/ /web/wsgi-scripts/
<Location /wsgi-scripts>
SetHandler wsgi-script
Options +ExecCGI
</Location>
<Directory /You/directory/Path>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /You/directory/Path/wsgi.py
</VirtualHost>

Or add this and something from youvirtualhost.conf to end of Your httpd.conf :

  WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup %{GLOBAL}

Alias /wsgi-scripts/ /web/wsgi-scripts/
<Location /wsgi-scripts>
SetHandler wsgi-script
Options +ExecCGI
</Location>

Also You can visit https://serverfault.com/q/281487 and https://modwsgi.readthedocs.org/en/develop/



Related Topics



Leave a reply



Submit