skip to Main Content

I have searched and read other posts with similar problems. I fixed the shebang line in the .py file and made sure that httpd.conf has correct config. Unfortunately, nothing has resolved my problem and I still get the dreaded errors –

[Mon Jun 01 10:37:02.994516 2020] [cgi:error] [pid 19596:tid 1196] (OS 1920)The file cannot be accessed by the system.  : [client ::1:50159] couldn't create child process: 721920: upload_file.py
[Mon Jun 01 10:37:02.994516 2020] [cgi:error] [pid 19596:tid 1196] (OS 1920)The file cannot be accessed by the system.  : [client ::1:50159] AH01223: couldn't spawn child process: C:/Users/raj_d/webroot/tsp_quick/cgi-bin/upload_file.py

The Python script –

#!"C:Usersraj_dAppDataLocalMicrosoftWindowsAppsPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0python.exe"
import cgi, os
import cgitb

cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
   # strip leading path from file name to avoid
   # directory traversal attacks
   fn = os.path.basename(fileitem.filename)
   open('/tmp/' + fn, 'wb').write(fileitem.file.read())
   message = 'The file "' + fn + '" was uploaded successfully'
else:
   message = 'No file was uploaded'
print """
Content-Type: text/htmln
<html>
<body>
   <p>%s</p>
</body>
</html>
""" % (message,)

I have played with adding and removing quotes on the full path in the shebang and that hasn’t helped.

I have these in httpd.conf –

LoadModule cgi_module modules/mod_cgi.so

<Directory "C:Usersraj_dwebroottsp_quickcgi-bin">
    AllowOverride None
    # Options None
    Options +ExecCGI
    AddHandler cgi-script .py
    Require all granted
</Directory>

I made sure that python was excutable from the shebang path –

C:>C:Usersraj_dAppDataLocalMicrosoftWindowsAppsPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0python.exe
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Any help will be appreciated.

Thanks in advance.

RD

2

Answers


  1. Chosen as BEST ANSWER

    Well, I figured it out.

    First and foremost, one should always follow the guidelines :)

    In my case, I gave up too quickly in running the script from command line. I had installed Python 3.8.3 from the Microsoft store and for some reason every time I ran the script from the command line, a new cmd window would popup and close very quickly. I was unable to change that behavior and couldn't see what was going wrong.

    So I uninstalled python, downloaded the new installer from python's site, and installed in a common location where my dev tools are installed. Now, I could run my script from command line and was able to see what the actual problem was.

    It turned out to be print! Here is the modified code which works just fine and the shebang became simpler as well.

    #!C:toolspython\python.exe
    import cgi, os
    import cgitb
    import time
    
    cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       # open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    a = """Content-Type: text/htmln
    <html>
    <body>
    <p>{}</p>
    </body>
    </html>
    """
    print (a.format(message))
    

  2. I had similar issues trying to execute *.py files in Apache on Windows. I followed the Apache CGI setup guide, but the problem (as you allude to) is with the Python executable in

    C:/Users/../AppData/Local/Microsoft/WindowsApps/Python/
    

    For some reason that I can’t figure out, Apache won’t allow you to use the Python interpreter in this location. I did as you suggested and uninstalled Python 3.10.x and re-installed on my D: drive and hey presto, it just worked.
    I suspect this is a windows-only quirk, probably a security issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search