lost and found ( for me ? )

Showing posts with label mod_python. Show all posts
Showing posts with label mod_python. Show all posts

apache mod_python : upload files over post method

many thanks!

root@ubuntu-vm2:~# tail -1 /etc/lsb-release ;uname -ri
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
3.2.0-49-virtual x86_64
root@ubuntu-vm2:~# apache2ctl -v
Server version: Apache/2.2.22 (Ubuntu)
Server built:   Jul 12 2013 13:37:10

on the apache server, place the following python script under /var/www/python_works directory.
root@ubuntu-vm2:~# cat /var/www/python_works/index.py
import os

s = """\
<html><body>
<h2>Hello %s!</h2>
</body></html>
"""

def index():
  return s % 'World'
  
def everybody():
  return s % 'everybody'

def form():
  return """\
<html><body>
<form enctype="multipart/form-data" action="./upload" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body></html>
"""

def upload(req):
  
  try: # Windows needs stdio set for binary mode.
     import msvcrt
     msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
     msvcrt.setmode (1, os.O_BINARY) # stdout = 1
  except ImportError:
     pass

  # A nested FieldStorage instance holds the file
  fileitem = req.form['file']

  # Test if the file was uploaded
  if fileitem.filename:

     # strip leading path from file name to avoid directory traversal attacks
     fname = os.path.basename(fileitem.filename)
     # build absolute path to files directory
     dir_path = os.path.join(os.path.dirname(req.filename), 'files')
     open(os.path.join(dir_path, fname), 'wb').write(fileitem.file.read())
     message = 'The file "%s" was uploaded successfully' % fname

  else:
     message = 'No file was uploaded'
  
  return """\
<html><body>
<p>%s</p>
<p><a href="./form">Upload another file</a></p>
</body></html>
""" % message
root@ubuntu-vm2:~#



make directory under /var/www/python_works.
uploaded files are copied to /var/www/python_works/files directory.
root@ubuntu-vm2:/var/www/python_works# pwd
/var/www/python_works
root@ubuntu-vm2:/var/www/python_works# mkdir files

root@ubuntu-vm2:/var/www/python_works# chmod 777 files/

access to http://apache IP/python_works/index.py/form


select a file you would like to upload to the apache server and then click “Upload”



15M.png was copied to /var/www/python_works/files directory.

root@ubuntu-vm2:~# ls /var/www/python_works/files/
15M.png


Apache : mod_python



Here’s how to use mod_pyhon on Ubuntu 12.04 apache.

just referred to the following tutorial.many thanks!


root@ubuntu-vm2:~# tail -1 /etc/lsb-release ;uname -ri
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
3.2.0-49-virtual x86_64

install apache2, mod_python
root@ubuntu-vm2:~# apt-get install apache2  libapache2-mod-python

to use python within Apache, there are two ways, publisher handler or PSP hander.

The Publisher handler allows access to functions and variables within a module via URLs.

The PSP handler processes text documents with Python code embedded between <% and %> tags

[ publisher handler ]

edit
# vi /etc/apache2/sites-available/default

add the red lines.
       </Directory>
       <Directory /var/www/>
               Options Indexes FollowSymLinks MultiViews
               AllowOverride None
               Order allow,deny

               # add for mod_python
               allow from all
               allow from allddHandler mod_python .py
               PythonHandler mod_python.publisher
               PythonDebug On
       </Directory>

when there is not “allow from all” line, I got the error “403 forbidden”

restart apache2
# service apache2 restart

make a python script file for testing.
root@ubuntu-vm2:~# cat /var/www/test.py
def index(req):
return "hello!";



root@ubuntu-vm2:~# ls -l /var/www/test.py
-rw-r--r-- 1 root root 34  7月 21 09:28 /var/www/test.py

access to http://Apache’s IP/test.py

[ PSP handler ]

edit default.
root@ubuntu-vm2:~# vi /etc/apache2/sites-available/default



DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny

# add for mod_python
# allow from all
# AddHandler mod_python .py
# allow from allddHandler mod_python .py
#               PythonHandler mod_python.publisher
#               PythonDebug On

# add for mod_python ( PSP handler )
allow from all
               AddHandler mod_python .psp
               PythonHandler mod_python.psp
               PythonDebug On
</Directory>



restart apache2
# service apache2 restart

prepare psp file

root@ubuntu-vm2:~# cat /var/www/test.psp
<html>
<body>
<h1><% req.write("Hello!") %></h1>
</body>
</html>
root@ubuntu-vm2:~#
root@ubuntu-vm2:~# ls -l /var/www/*.psp
-rw-r--r-- 1 root root 65  7月 21 12:25 /var/www/test.psp



access to the psp file.