PlexKodiConnect/resources/lib/plexbmchelper/httppersist.py

104 lines
3.7 KiB
Python
Raw Normal View History

2016-09-03 01:20:19 +10:00
import logging
2016-01-15 22:12:52 +11:00
import httplib
import traceback
import string
import errno
from socket import error as socket_error
2016-09-03 01:20:19 +10:00
###############################################################################
log = logging.getLogger("PLEX."+__name__)
###############################################################################
2016-01-15 22:12:52 +11:00
2016-01-15 22:12:52 +11:00
class RequestMgr:
def __init__(self):
self.conns = {}
def getConnection(self, protocol, host, port):
conn = self.conns.get(protocol+host+str(port), False)
if not conn:
if protocol == "https":
2016-01-15 22:12:52 +11:00
conn = httplib.HTTPSConnection(host, port)
else:
conn = httplib.HTTPConnection(host, port)
self.conns[protocol+host+str(port)] = conn
return conn
2016-01-15 22:12:52 +11:00
def closeConnection(self, protocol, host, port):
conn = self.conns.get(protocol+host+str(port), False)
if conn:
conn.close()
self.conns.pop(protocol+host+str(port), None)
2016-01-15 22:12:52 +11:00
def dumpConnections(self):
for conn in self.conns.values():
conn.close()
self.conns = {}
2016-01-15 22:12:52 +11:00
def post(self, host, port, path, body, header={}, protocol="http"):
conn = None
try:
conn = self.getConnection(protocol, host, port)
header['Connection'] = "keep-alive"
conn.request("POST", path, body, header)
data = conn.getresponse()
if int(data.status) >= 400:
2016-09-03 01:20:19 +10:00
log.error("HTTP response error: %s" % str(data.status))
# this should return false, but I'm hacking it since iOS
# returns 404 no matter what
2016-01-15 22:12:52 +11:00
return data.read() or True
else:
2016-01-15 22:12:52 +11:00
return data.read() or True
except socket_error as serr:
# Ignore remote close and connection refused (e.g. shutdown PKC)
if serr.errno in (errno.WSAECONNABORTED, errno.WSAECONNREFUSED):
pass
else:
2016-09-03 01:20:19 +10:00
log.error("Unable to connect to %s\nReason:" % host)
log.error(traceback.print_exc())
2016-01-15 22:12:52 +11:00
self.conns.pop(protocol+host+str(port), None)
if conn:
conn.close()
return False
except Exception as e:
log.error("Exception encountered: %s" % e)
# Close connection just in case
try:
conn.close()
except:
pass
return False
def getwithparams(self, host, port, path, params, header={},
protocol="http"):
2016-01-15 22:12:52 +11:00
newpath = path + '?'
pairs = []
for key in params:
pairs.append(str(key)+'='+str(params[key]))
newpath += string.join(pairs, '&')
return self.get(host, port, newpath, header, protocol)
2016-01-15 22:12:52 +11:00
def get(self, host, port, path, header={}, protocol="http"):
try:
conn = self.getConnection(protocol, host, port)
header['Connection'] = "keep-alive"
conn.request("GET", path, headers=header)
data = conn.getresponse()
if int(data.status) >= 400:
2016-09-03 01:20:19 +10:00
log.error("HTTP response error: %s" % str(data.status))
2016-01-15 22:12:52 +11:00
return False
else:
2016-01-15 22:12:52 +11:00
return data.read() or True
except socket_error as serr:
# Ignore remote close and connection refused (e.g. shutdown PKC)
if serr.errno in (errno.WSAECONNABORTED, errno.WSAECONNREFUSED):
pass
else:
2016-09-03 01:20:19 +10:00
log.error("Unable to connect to %s\nReason:" % host)
log.error(traceback.print_exc())
2016-01-15 22:12:52 +11:00
self.conns.pop(protocol+host+str(port), None)
conn.close()
return False