UrlCallAction
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 2
c 1
b 0
f 0
wmc 0
1
# -*- coding: utf-8 -*-
2
# thx to pula @ DoorPi forum
3
# https://www.doorpi.org/forum/thread/25-http-request/?postID=596#post596
4
5
import logging
6
logger = logging.getLogger(__name__)
7
logger.debug("%s loaded", __name__)
8
9
from doorpi.action.base import SingleAction
10
import doorpi
11
12
import urllib2
13
import ssl
14
import urlparse
15
16
17
def fire_command(url):
18
    try:
19
        if "@" in url:
20
            nurl = urlparse.urlsplit(url)
21
            username = nurl.username
22
            password = nurl.password
23
            url = url.replace(username + ':' + password + '@', '')
24
            url = url.replace(" ", "%20")
25
            logger.debug('url: %s' % url)
26
            ssl._create_default_https_context = ssl._create_unverified_context
27
            p = urllib2.HTTPPasswordMgrWithDefaultRealm()
28
            p.add_password(None, url, username, password)
29
            handler = urllib2.HTTPBasicAuthHandler(p)
30
            opener = urllib2.build_opener(handler)
31
            urllib2.install_opener(opener)
32
            url = url.replace(" ", "%20")
33
            logger.info('url: %s' % url)
34
            return urllib2.urlopen(
35
                url=url,
36
                data=None,
37
                timeout=1
38
            )
39
    except urllib2.HTTPError as exp:
40
        logger.error('HTTPError: %s - %s' % (exp.code, exp.reason))
41
    except urllib2.URLError as exp:
42
        logger.error('URLError: %s' % exp.reason)
43
    return False
44
45
46
def get(parameters):
47
    parsed_parameters = doorpi.DoorPi().parse_string(parameters)
48
    return UrlCallAction(fire_command, url=parsed_parameters)
49
50
51
class UrlCallAction(SingleAction):
52
    pass
53