1515 from urllib .parse import urlencode
1616import xml .etree .ElementTree as ET
1717from threading import Thread
18+ try :
19+ import ssl
20+ ssl_enabled = True
21+ except ImportError :
22+ ssl_enabled = False
1823
1924# Foscam error code.
2025FOSCAM_SUCCESS = 0
@@ -37,7 +42,7 @@ def __str__(self):
3742class FoscamCamera (object ):
3843 '''A python implementation of the foscam HD816W'''
3944
40- def __init__ (self , host , port , usr , pwd , daemon = False , verbose = True ):
45+ def __init__ (self , host , port , usr , pwd , daemon = False , ssl = None , verbose = True ):
4146 '''
4247 If ``daemon`` is True, the command will be sent unblockedly.
4348 '''
@@ -47,6 +52,12 @@ def __init__(self, host, port, usr, pwd, daemon=False, verbose=True):
4752 self .pwd = pwd
4853 self .daemon = daemon
4954 self .verbose = verbose
55+ self .ssl = ssl
56+ if ssl_enabled :
57+ if port == 443 and ssl is None :
58+ self .ssl = True
59+ if self .ssl is None :
60+ self .ssl = False
5061
5162 @property
5263 def url (self ):
@@ -68,13 +79,19 @@ def send_command(self, cmd, params=None, raw=False):
6879 cmd ,
6980 paramstr ,
7081 )
82+ if self .ssl and ssl_enabled :
83+ cmdurl = cmdurl .replace ('http:' ,'https:' )
7184
7285 # Parse parameters from response string.
7386 if self .verbose :
7487 print ('Send Foscam command: %s' % cmdurl )
7588 try :
7689 raw_string = ''
77- raw_string = urlopen (cmdurl ).read ()
90+ if self .ssl and ssl_enabled :
91+ gcontext = ssl .SSLContext (ssl .PROTOCOL_TLSv1 ) # disable cert
92+ raw_string = urlopen (cmdurl ,context = gcontext ).read ()
93+ else :
94+ raw_string = urlopen (cmdurl ).read ()
7895 if raw :
7996 if self .verbose :
8097 print ('Returning raw Foscam response: len=%d' % len (raw_string ))
0 commit comments