Python Multipart Posts

To use a camera with the Raspberry PI, first [configure the Raspberry PI] to enable the camera.

In order to send large images from the Raspberry PI camera to a web server, the image has to be sent in parts. This requires the Python `poster` module.

To be able to install a python module, `pip` has to be installed.

sudo pip install poster

This is the server PHP page that saves the image.



$image = $_FILES["image"];

if ($image == null) {
   echo "Missing image to save!";
} else {
   echo "Saved image!";
   $tmp_name = $_FILES["image"]["tmp_name"];
   move_uploaded_file($tmp_name, "image.jpg");


This Python script sends the image to the server.

#!/usr/bin/env python

import urllib, urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers


url = ""
print "url="+url

filename = 'image.jpg';
if (os.path.isfile(filename)) :
  values = {'image':open(filename)}
  data, headers = multipart_encode(values)
  headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
  req = urllib2.Request(url, data, headers)
  req.unverifiable = True
  content = urllib2.urlopen(req).read()
  print (content)
  print 'No image file found to upload';

print '\r\nProgam complete.'

This script will capture from the camera and then call the above script to upload the image.

#get access to the camera
from picamera import PiCamera

#import so we can invoke another script
import subprocess

#sleep so we can wait on the camera
from time import sleep

# create a camera object
camera = PiCamera()

#set the image resolution
camera.resolution = (320, 240)

#rotate the camera if upside-down
camera.rotation = 180

#show preview with some transparency

#wait two seconds for camera lighting

#save image locally

#stop the preview

#invoke the script to upload the image'python', shell=True)