A better Python script for uploading images

[RPi-Cam-Web-Interface]

Install the [poster] module by running the command-line:

sudo pip install poster

UploadImage.py (RPI)

#!/usr/bin/env python

import urllib, urllib2, os, os.path, sys
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
from time import sleep

register_openers()

query = { 'id' : 1 }
url = "http://domain.com/path/save_image.php?"+urllib.urlencode(query)

filename = '/var/www/html/cam.jpg'
#print 'Saved: '+filename;

while True:
  try:
    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)'
      headers['filename'] = filename
      req = urllib2.Request(url, data, headers)
      req.unverifiable = True
      content = urllib2.urlopen(req).read()
  except:
    print 'Upload failed.';
  sleep(1)

Edit ~/.bashrc (RPI) and add the following to the end of the file.

python ~/Documents/UploadImage.py &

Reboot the Raspberry PI and the security camera is ready to go!

save_image.php (Server)

<?php

if (!isset($_GET['id'])) {
  echo ('Invalid request!');
  exit(0);
}
$id = intval($_GET['id']);

$image = $_FILES["image"];

if ($image == null) {
   echo ("Missing image!");
   exit(0);
} else {
   echo "Saved image!";
   $filename = "image" . $id . ".jpg";
   $tmp_name = $_FILES["image"]["tmp_name"];
   move_uploaded_file($tmp_name, $filename);
}
?>

[Setup wifi on the command-line]