UpnpPunch gets custom logger
This commit is contained in:
parent
13d1df3cef
commit
e97873fb7e
1 changed files with 9 additions and 8 deletions
|
@ -15,6 +15,7 @@ import gevent
|
||||||
# General TODOs:
|
# General TODOs:
|
||||||
# Handle 0 or >1 IGDs
|
# Handle 0 or >1 IGDs
|
||||||
|
|
||||||
|
logger = logging.getLogger("Upnp")
|
||||||
|
|
||||||
class UpnpError(Exception):
|
class UpnpError(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -216,10 +217,10 @@ def _create_close_message(local_ip,
|
||||||
|
|
||||||
|
|
||||||
def _parse_for_errors(soap_response):
|
def _parse_for_errors(soap_response):
|
||||||
logging.debug(soap_response.status)
|
logger.debug(soap_response.status)
|
||||||
if soap_response.status >= 400:
|
if soap_response.status >= 400:
|
||||||
response_data = soap_response.read()
|
response_data = soap_response.read()
|
||||||
logging.debug(response_data)
|
logger.debug(response_data)
|
||||||
try:
|
try:
|
||||||
err_dom = parseString(response_data)
|
err_dom = parseString(response_data)
|
||||||
err_code = _get_first_child_data(err_dom.getElementsByTagName(
|
err_code = _get_first_child_data(err_dom.getElementsByTagName(
|
||||||
|
@ -249,7 +250,7 @@ def _send_soap_request(location, upnp_schema, control_path, soap_fn,
|
||||||
),
|
),
|
||||||
'Content-Type': 'text/xml'
|
'Content-Type': 'text/xml'
|
||||||
}
|
}
|
||||||
logging.debug("Sending UPnP request to {0}:{1}...".format(
|
logger.debug("Sending UPnP request to {0}:{1}...".format(
|
||||||
location.hostname, location.port))
|
location.hostname, location.port))
|
||||||
conn = http.client.HTTPConnection(location.hostname, location.port)
|
conn = http.client.HTTPConnection(location.hostname, location.port)
|
||||||
conn.request('POST', control_path, soap_message, headers)
|
conn.request('POST', control_path, soap_message, headers)
|
||||||
|
@ -280,7 +281,7 @@ def _send_requests(messages, location, upnp_schema, control_path):
|
||||||
|
|
||||||
|
|
||||||
def _orchestrate_soap_request(ip, port, msg_fn, desc=None, protos=("TCP", "UDP")):
|
def _orchestrate_soap_request(ip, port, msg_fn, desc=None, protos=("TCP", "UDP")):
|
||||||
logging.debug("Trying using local ip: %s" % ip)
|
logger.debug("Trying using local ip: %s" % ip)
|
||||||
idg_data = _collect_idg_data(ip)
|
idg_data = _collect_idg_data(ip)
|
||||||
|
|
||||||
soap_messages = [
|
soap_messages = [
|
||||||
|
@ -309,7 +310,7 @@ def _communicate_with_igd(port=15441,
|
||||||
_orchestrate_soap_request(local_ip, port, fn, desc, protos)
|
_orchestrate_soap_request(local_ip, port, fn, desc, protos)
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.debug('Upnp request using "{0}" failed: {1}'.format(local_ip, e))
|
logger.debug('Upnp request using "{0}" failed: {1}'.format(local_ip, e))
|
||||||
gevent.sleep(1)
|
gevent.sleep(1)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -341,7 +342,7 @@ def _communicate_with_igd(port=15441,
|
||||||
|
|
||||||
|
|
||||||
def ask_to_open_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "UDP")):
|
def ask_to_open_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "UDP")):
|
||||||
logging.debug("Trying to open port %d." % port)
|
logger.debug("Trying to open port %d." % port)
|
||||||
_communicate_with_igd(port=port,
|
_communicate_with_igd(port=port,
|
||||||
desc=desc,
|
desc=desc,
|
||||||
retries=retries,
|
retries=retries,
|
||||||
|
@ -350,7 +351,7 @@ def ask_to_open_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "UD
|
||||||
|
|
||||||
|
|
||||||
def ask_to_close_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "UDP")):
|
def ask_to_close_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "UDP")):
|
||||||
logging.debug("Trying to close port %d." % port)
|
logger.debug("Trying to close port %d." % port)
|
||||||
# retries=1 because multiple successes cause 500 response and failure
|
# retries=1 because multiple successes cause 500 response and failure
|
||||||
_communicate_with_igd(port=port,
|
_communicate_with_igd(port=port,
|
||||||
desc=desc,
|
desc=desc,
|
||||||
|
@ -362,7 +363,7 @@ def ask_to_close_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "U
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from gevent import monkey
|
from gevent import monkey
|
||||||
monkey.patch_all()
|
monkey.patch_all()
|
||||||
logging.getLogger().setLevel(logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
import time
|
import time
|
||||||
|
|
||||||
s = time.time()
|
s = time.time()
|
||||||
|
|
Loading…
Reference in a new issue