Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import inspect
- import os.path
- import urllib2
- import ConfigParser
- class FoscamCamera(object):
- def __init__(self, url='', user='', pwd=''):
- self._user = user
- self._pwd = pwd
- self._url = url
- def readconfig(self):
- config = ConfigParser.ConfigParser(allow_no_value=True)
- sourcePath = os.path.dirname(inspect.getsourcefile(self.readconfig))
- foscamConfigFile = os.path.join(sourcePath,'foscamcamera.cfg')
- #Check if ConfigFile exists
- if not os.path.isfile(foscamConfigFile):
- configExists = False
- print '\nERROR: Missing Foscam configfile\n'
- else:
- configExists = True
- #Read Config
- if configExists:
- config.read(foscamConfigFile)
- try:
- self._host = config.get('Foscam', 'Host')
- self._port = config.get('Foscam', 'Port')
- self._user = config.get('Foscam', 'User')
- self._pwd = config.get('Foscam', 'Password')
- self._url = self._host + ':' + self._port
- except ConfigParser.NoOptionError, err:
- print '\nERROR: Could not parse config file\n'
- if self._host and self._port and self._user and self._pwd:
- configRead = True
- else:
- configRead = False
- return configExists and configRead
- def setURL(self, url):
- self._url = url
- def url(self):
- return self._url
- def setUser(self, usr):
- self._user = usr
- def user(self):
- return self._user
- def setPassword(self, pwd):
- self._pwd = pwd
- def password(self):
- return self._pwd
- def sendCommand(self, cgi, parameterDict):
- url = 'http://%s/%s?user=%s&pwd=%s' % (self.url(),
- cgi,
- self.user(),
- self.password())
- for param in parameterDict:
- url = url + '&%s=%s' % (param, parameterDict[param])
- return urllib2.urlopen(url)
- def snapshot(self):
- f = self.sendCommand('snapshot.cgi', {})
- return f.read()
- def snapshotToFile(self, filename):
- data = self.snapshot()
- open(filename, 'wb').write(data)
- if __name__ == '__main__':
- print
- print 'testing the Foscam camera code'
- print
- foscam = FoscamCamera()
- foscam.readconfig()
- foscam.snapshotToFile('snapshotnew.jpg')
- print 'wrote snapshot'
Advertisement
Add Comment
Please, Sign In to add comment