#! /usr/bin/python """ ncpng.py -- A Python program to upload .png overlay graphics files This program allows uploading of .png graphics files to a Telepresenter. It requires installation and use of the Python programming language. The most recent versions of the language may be installed by visiting "www.python.org" and downloading the correct binaries for Windows, Linux, Macs or other platforms. Sample usage: ncpng.py -1 graphic1.png telepresenter.ncast.com ncpng.py --g1=graphic1.png --g2=graphic2.png telepresenter.ncast.com Complete specification: ncpng.py -d -h -p password -v -1 file1.png -2 file2.png -3 file3.png -4 file4.png --debug --help --g1=file1.png --g2=file2.png --g3=file3.png --g4=file4.png --pswd=password --version host where -d, --debug Turn debug statements on -h, --help Print usage information -p, --pswd=password Use "password" -v, --version Report program version -1, --g1 Overlay graphic 1 -2, --g2 Overlay graphic 2 -3, --g3 Overlay graphic 3 -4, --g4 Overlay graphic 4 host ... Telepresenter to receive file(s) To run this program under Windows you may use a command line, such as: C:> "C:\Program Files\Python24\python.exe" ncpng.py -1 file.png 192.168.0.5 A simpler alternative is to create a shortcut to "ncpng.py" and then using the Properties tab for the shortcut enter the required arguments: "C:\Documents and Settings\owner\My Documents\Python\ncpng.py" -1 file.png 192.168.0.5 Please send comments or questions to "info@ncast.com". Copyright (2007) NCast Corporation All rights reserved """ import sys import os, os.path import time import getopt import string import urllib import urllib2 import socket def Upload(host, user, pswd, realm, gfile, graphic): # Create an OpenerDirector with support for Cookies ... cookie_handler = urllib2.HTTPCookieProcessor() opener = urllib2.build_opener(cookie_handler) # ...and install it globally so it can be used with urlopen. urllib2.install_opener(opener) url = "http://" + host + "/main.cgi" data = 'action=login&user=' + user + '&password=' + pswd req = urllib2.Request(url=url, data=data) try: report = urllib2.urlopen(req) except urllib2.URLError, msg: print "ncpng: Urllib2 error (%s)" % msg return False except socket.error, (errno, strerror): print "ncpng: Socket error (%s) for host %s (%s)" % (errno, host, strerror) return False url = "http://" + host + "/upload.cgi" data = '' separator = '---------------------7d7781f303ca' for i in range(0,4): # Create mime part for this file data = data + separator + '\r\n' data = data + 'Content-Disposition: form-data; name="overlay%s_file"; filename="%s"\r\n' % (i+1, gfile[i]) if len(graphic[i]) > 0: print 'ncpng: Uploading graphic file %d, length(%d), name="%s"' % (i+1, len(graphic[i]), gfile[i]) data = data + 'Content-Type: image/x-png\r\n\r\n' data = data + graphic[i] + '\r\n' else: data = data + 'Content-Type: application/octet-stream\r\n\r\n' data = data + separator + '--' + '\r\n' req = urllib2.Request(url=url, data=data) req.add_header('Content-type','multipart/form-data; boundary=' + separator) try: report = urllib2.urlopen(req) except urllib2.URLError, msg: print "ncpng: Urllib2 error (%s)" % msg return False except socket.error, (errno, strerror): print "ncpng: Socket error (%s) for host %s (%s)" % (errno, host, strerror) return False return True # Main Program ------------------------------------------------------- host = None debug = False gfile = ["", "", "", ""] graphic = ["", "", "", ""] graphics = False user = "admin" pswd = "ncast" realm = "NCast M3" def Usage(): print "Usage: ncpng.py -d -h -p password -v -1 file1.png ... --debug --g1=file1.png ... --help --pswd=password --version host" try: options, args = getopt.getopt(sys.argv[1:], 'dhp:v1:2:3:4:', ['debug', 'g1=', 'g2=', 'g3=', 'g4=', 'help', 'pswd=', 'version']) except getopt.GetoptError: Usage() sys.exit(2) for o, a in options: if o in ("-d", "--debug"): debug = True if o in ("-h", "--help"): Usage() sys.exit() if o in ("-p", "--pswd"): pswd = a if o in ("-v", "--version"): print "ncpng.py Version 1.0" sys.exit() if o in ("-1", "--g1"): gfile[0] = a if o in ("-2", "--g2"): gfile[1] = a if o in ("-3", "--g3"): gfile[2] = a if o in ("-4", "--g4"): gfile[3] = a if len(args) == 1: host = args[0] else: print "ncpng: hostname error %s" % args sys.exit(1) for i in range(0, 4): if len(gfile[i]) > 0 : try: f = open(gfile[i], 'r') graphic[i] = f.read() f.close() print "ncpng: Uploading file %s" % gfile[i] graphics = True except IOError, (errno, strerror): print "ncpng I/O error(%s) on file %s: %s" % (errno, gfile[i], strerror) sys.exit(1) if not graphics: print "ncpng: No graphics files to upload. Exiting." sys.exit(1) if host: print "ncpng: Uploading to this host:", host else: print "ncpng: No host listed for upload" sys.exit(1) result = Upload(host, user, pswd, realm, gfile, graphic) print "ncpng: Upload complete"