Mailing List Archive

symlinks with python3 http.server.CGIHTTPRequestHandler
Is it possible to get http.server.CGIHTTPRequestHandler to run a symlink-ed
script?

In the example below, GET /cgi-bin/test.py results in a 404 because it is a
symlink.

% mkdir -p test/cgi-bin
% cd test
% vi test.py
% chmod +x test.py
% ln -s test.py cgi-bin
% cp test.py cgi-bin/test2.py
% chmod +x cgi-bin/test2.py
% python3 -m http.server --cgi 8090
Serving HTTP on :: port 8090 (http://[::]:8090/) ...
::1 - - [06/Jan/2022 09:21:48] code 404, message No such CGI script
('/cgi-bin/test.py')
::1 - - [06/Jan/2022 09:21:48] "GET /cgi-bin/test.py HTTP/1.1" 404 -
::1 - - [06/Jan/2022 09:21:59] "GET /cgi-bin/test2.py HTTP/1.1" 200 -
--
https://mail.python.org/mailman/listinfo/python-list
Re: symlinks with python3 http.server.CGIHTTPRequestHandler [ In reply to ]
Hi

Maybe you have some restrictions on file system level. Some selinux for
example.


I try similar steps on my local 'linux mint' and ... linked script is
called my http.server without errors.


Here is my 'tree':

??? cgi-bin
?   ??? test.py -> ../orig.py
??? orig.py

All files are executable and test.py is link to orig.py which is in one
directory level up:

$ ls -Rla
.:
total 16
drwxrwxr-x  3 kirill kirill 4096 Jan 11 14:04 .
drwxrwxrwt 23 root   root   4096 Jan 11 14:11 ..
drwxrwxr-x  2 kirill kirill 4096 Jan 11 14:03 cgi-bin
-rwxrwxr-x  1 kirill kirill   31 Jan 11 14:04 orig.py

./cgi-bin:
total 8
drwxrwxr-x 2 kirill kirill 4096 Jan 11 14:03 .
drwxrwxr-x 3 kirill kirill 4096 Jan 11 14:04 ..
lrwxrwxrwx 1 kirill kirill   10 Jan 11 14:03 test.py -> ../orig.py

I do 'curl' request:

curl -v http://0.0.0.0:8000/cgi-bin/test.py

And http.server get log:

$ python3 -m http.server --cgi 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [11/Jan/2022 14:40:19] "GET /cgi-bin/test.py HTTP/1.1" 200 -


On 1/10/22 22:10, Nat Taylor wrote:
> python3 -m http.server --cgi 8090
--
https://mail.python.org/mailman/listinfo/python-list
Re: symlinks with python3 http.server.CGIHTTPRequestHandler [ In reply to ]
On Tue, Jan 11, 2022 at 6:17 AM Nat Taylor <nattaylor@gmail.com> wrote:
>
> Is it possible to get http.server.CGIHTTPRequestHandler to run a symlink-ed
> script?
>
> In the example below, GET /cgi-bin/test.py results in a 404 because it is a
> symlink.
>
> % mkdir -p test/cgi-bin
> % cd test
> % vi test.py
> % chmod +x test.py
> % ln -s test.py cgi-bin

I just noticed something. This might not produce the effect you want;
a symlink aims at a specific named target, and if you use a relative
path, it's relative to the directory containing the symlink, not the
directory you were in when you created it. What happens if, instead,
you do this:

% cd cgi-bin
% ln -s ../test.py .

? Conversely, can you, with whichever setup you're looking at, read
the source code for the script by catting it from cgi-bin? It might be
that the link is actually unreadable or pointing to the wrong place.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list