How do I upload a file with mod_python?

I want to create a simple file upload form and I must be completely incapable. I've read docs and tutorials,but for some reason, I'm not getting the submitted form data. I wrote the smallest amount of code I could to test and it still isn't working. Any ideas what's wrong?

def index():
    html = '''
      <form id="fileUpload" action="./result" method="post">
        <input type="file" id="file"/>
        <input type="submit" value="Upload"/>
    return html

def result(req):
    try: tmpfile = req.form['file']
        return "no file!"

Asked by: Roman315 | Posted: 06-12-2021

Answer 1

try putting enctype="multipart/form-data" in your form tag. Your mistake is not really mod_python related.

Answered by: Lenny861 | Posted: 07-01-2022

Similar questions

python - How do you throw an HTTP error with mod_python

I have a setup where I'm serving simple python pages using the mod_python publisher. At some points I'd like to have the python function raise a standard apache error - for example throwing a 500 error if a required file is missing. How can I throw an apache error from within a mod_python script?

python - Error in mod_python code!

import cgi def fill(): s = """\ &lt;html&gt;&lt;body&gt; &lt;form method="get" action="./show"&gt; &lt;p&gt;Type a word: &lt;input type="text" name="word"&gt; &lt;input type="submit" value="Submit"&lt;/p&gt; &lt;/form&gt;&lt;/body&gt;&lt;/html&gt; """ return s # Receive the Request object def show(req): # The getfirst() method returns the value of the first field with the # name passed as the method a...

python - How do I delete mod_python?

I keep getting these errors in my Apache logs: [error] python_init: Python version mismatch, expected '2.6.5+', found '2.6.6'. [error] python_init: Python executable found '/usr/bin/python'. [error] python_init: Python path being used '/usr/lib/python2.6/:/usr/lib/python2.6/plat-linux2:/usr/lib/python2.6/lib-tk:/usr/lib/python2.6/lib-old:/usr/lib/python2.6/lib-dynload'. [notice] mod_python: Creating 8 sessi...

python - Unable to write to files with mod_python and PSP

So, I have a Python Server Pages script that when ran, saves stuff to a file in /var/www/. It opens the file with: open(filename, 'wb') Which causes this: IOError: [Errno 13] Permission denied: 'file.txt' I'm using Apache and have set /var/www/ to chmod 755. I'm not sure if www-data is trying to write, since when I ru...

python - Getting Error when trying to setup mod_python

Closed. This question is off-topic. It is not curre...

python - got errors when import mod_python

After finishing install mod_python, I got 500 Internal Server Error. I looked up the log, it says: python_handler: Can't get/create interpreter. Then I open a python terminal and to test if I can import mod_python. Then I got errors as follows: &gt;&gt;&gt; import mod_python Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "/usr/local/lib/python2.7/dist-pa...

python - Can I run flask on an Apache server with mod_python?

I have a pre-configured Apache server with neither root nor any shell access. I only can push files via ftp. However, I know that the server supports Python via mod_python. Here is what the config says: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny16 with Suhosin-Patch mod_python/3.3.1 Python/2.5.2 I wonder whether I can run flask...

python - How to send data via POST or GET in Mod_Python?

With JS, i send a AJAX post request. $.ajax( {method:"POST", url:"https://my/website/", data:JSON.stringify(data), contentType: 'application/json;charset=UTF-8' On my Apache2 mod_Python server, I wish for my python file to access data. How can i do this? def index(req): # data = ?? PS: he...

python - Django, mod_python, apache and wacky sessions

I am running a Django through mod_python on Apache on a linux box. I have a custom authentication backend, and middleware that requires authentication for all pages, except static content. My problem is that after I log in, I will still randomly get the log in screen now and again. It seems to me that each apache process has it's own python process, which in turn has it's own internals. So as long as I get served b...

python - Failed to get separate instances of a class under mod_python

I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually the code does os.fork() and spawns 2 separate long-run processes. Each of those processes has to create a separate instance of a class in order to avoid any possible collision in the parallel flow. class Foo(object): pass kidprocs = [] for kid in ('kid1', 'kid2'): pid = os.fork() if pid: # parent kidprocs.a...

python - Running Django with FastCGI or with mod_python

which would you recommend? which is faster, reliable? apache mod_python or nginx/lighttpd FastCGI?

python - How to access to the root path in a mod_python directory?

In my Apache webserver I put this: &lt;Directory /var/www/; SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On &lt;/Directory&gt; Then I have a file with an index function. When I go to, I see a web page produced by the index function (just a plain vanilla HTML page). Every other page is of th...

python - How do I modify sys.path from .htaccess to allow mod_python to see Django?

The host I'm considering for hosting a Django site has mod_python installed, but does not have Django. Django's INSTALL file indicates that I can simply copy the django directory to Python's site-packages directory to install Django, so I suspect that it might be possible to configure Python / mod_python to look for it elsewhere (namely my user space) by modifying sys.path, but I don't know how to change it from .htaccess ...

php - mod_php vs mod_python

Why mod_python is oop but mod_php is not ? Example :We go to if you use mod_python apache opens www/ and calls dir2 method but if you use php module apache opens www/dir1/dir2/index.php

python - mod_python publisher and pretty URLs

I am new to Python (I am getting out of PHP because of how increasingly broken it is), and I am racing through porting my old code. One thing: I have a file / with functions index() and bar(), so, with the publisher I can access http://domain/foo/bar and http://domain/foo as the documentatio...

python - Configure Apache to recover from mod_python errors

This question already has an answer here:

python - mod_python caching of variables

I'm using mod_python to run Trac in Apache. I'm developing a plugin and am not sure how global variables are stored/cached. I am new to python and have googled the subject and found that mod_python caches python modules (I think). However, I would expect that cache to be reset when the web service is restarted, but it doesn't appear to be. I'm saying this becasue I have a global variable that is a ...

django - Different behavior of python logging module when using mod_python

We have a nasty problem where we see that the python logging module is behaving differently when running with mod_python on our servers. When executing the same code in the shell, or in django with the runserver command or with mod_wsgi, the behavior is correct: import logging logger = logging.getLogger('site-errors') logging.debug('logger=%s' % (logger.__dict__)) logging.debug('logger.parent=%s' % (logger....

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python