Skip to content

Commit d76f5b5

Browse files
authored
Merge pull request #10 from texnofobix/master
Adding SSL Support (#1)
2 parents 09533fe + 19e3c60 commit d76f5b5

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

foscam/foscam.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
from urllib.parse import urlencode
1616
import xml.etree.ElementTree as ET
1717
from threading import Thread
18+
try:
19+
import ssl
20+
ssl_enabled=True
21+
except ImportError:
22+
ssl_enabled=False
1823

1924
# Foscam error code.
2025
FOSCAM_SUCCESS = 0
@@ -37,7 +42,7 @@ def __str__(self):
3742
class 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

Comments
 (0)