hospitality.thecloud.eu_mercure_free.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python3
  2. import subprocess
  3. import re
  4. import lxml
  5. from lxml import html
  6. import urllib.request
  7. import urllib3
  8. import logging
  9. import subprocess
  10. headers = {}
  11. headers['User-Agent'] = 'Mozilla/5.0'
  12. pool = urllib3.PoolManager()
  13. defaultdummy='google.com'
  14. class ConnectStateExplorer:
  15. def __init__(self):
  16. self.dummy = defaultdummy
  17. def explore_init(self):
  18. logging.info("explore: init")
  19. (r,s,h,redir) = self.get(url=self.dummy, headers=headers)
  20. parsed_redir = urllib.parse.urlparse(redir)
  21. parsed_dummy = urllib.parse.urlparse(self.dummy)
  22. redirected = parsed_redir.netloc != parsed_dummy.netloc
  23. logging.info("code %s" % s)
  24. logging.info("was redirected? %s" % redirected)
  25. logging.info("connected? %s" % self.test_connection_ping())
  26. x = self.find_html_form_elems(h)
  27. if not isinstance(x, tuple) or not x[0] == "ok":
  28. logging.info("couldn't interpret / find suitable form in redirected html")
  29. logging.debug(h)
  30. formaction = x[1]
  31. fields = x[2]
  32. if urllib.parse.urlparse(formaction).netloc == '':
  33. formaction = "https://" + parsed_redir.netloc + "/" + re.sub("^/", "", formaction)
  34. self.post_form(url=formaction, headers=headers, fields=fields)
  35. logging.info("connected? %s" % self.test_connection_ping())
  36. def find_html_form_elems(self, text):
  37. try:
  38. hdoc = html.document_fromstring(text)
  39. inputs = hdoc.xpath('//input')
  40. kvs = [(elt.attrib.get('name'),
  41. elt.attrib.get('value')) for elt in inputs if elt.attrib.get('type') == 'hidden']
  42. chk = [(elt.attrib.get('name'), "checked") for elt in inputs if elt.attrib.get('type') == 'checkbox']
  43. forms = hdoc.xpath('//form')
  44. if len(forms) == 0:
  45. return False
  46. action = forms[0].attrib.get('action')
  47. fields = dict(kvs)
  48. if len(chk) > 0:
  49. fields[chk[0][0]] = chk[0][1]
  50. return ("ok", action, fields)
  51. except Exception as e:
  52. logging.info("Exception interpreting HTML, %s" % e)
  53. return False
  54. def post_form(self, **kwargs):
  55. logging.info("requesting %s" % kwargs)
  56. f3 = pool.request_encode_body(method='POST', **kwargs)
  57. # print(f3.getheaders())
  58. # print(f3.data)
  59. def get(self, **kwargs):
  60. logging.info("requesting %s" % kwargs)
  61. r = pool.request(method="GET", **kwargs)
  62. s = r.status
  63. h = r.data
  64. redir = r.geturl()
  65. return (r, s, h, redir)
  66. def test_connection_ping(self, destination="google.com"):
  67. retval = subprocess.call(["ping", destination, "-c", "1", "-W", "2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  68. return retval == 0
  69. def try_dummy(self):
  70. (r, s, h) = self.get(method='GET', url=dummy, headers=headers)
  71. d = html.document_fromstring(h)
  72. self.dummyresponse = r
  73. if __name__=="__main__":
  74. logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
  75. cse = ConnectStateExplorer()
  76. cse.explore_init()