1
0

wifiecn.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. print("Start Wifi ECN login tool ...")
  2. import urllib.request
  3. import urllib.parse
  4. import sys
  5. import getopt
  6. import os
  7. import json
  8. dir_path = os.path.dirname(os.path.realpath(__file__))
  9. username = ""
  10. password = ""
  11. argv = sys.argv[1:]
  12. try:
  13. opts, args = getopt.getopt(argv, "hu:p:", ["username=", "password="])
  14. except getopt.GetoptError:
  15. print('wifiecn.py -u <username> -p <password>')
  16. sys.exit(2)
  17. for opt, arg in opts:
  18. if opt == '-h':
  19. print('wifiecn.py -u <username> -p <password>')
  20. sys.exit()
  21. elif opt in ("-u", "--username"):
  22. username = arg
  23. elif opt in ("-p", "--password"):
  24. password = arg
  25. if username == "" or password == "":
  26. print('wifiecn.py -u <username> -p <password>')
  27. sys.exit(2)
  28. data = {"action": "authenticate",
  29. "from_ajax": "true",
  30. "login": username,
  31. "password": password,
  32. "policy_accept": "true",
  33. "wispr_mode": "false"}
  34. headers = {
  35. 'origin': "https://portailwifi.ec-nantes.fr/",
  36. 'upgrade-insecure-requests': "1",
  37. 'user-agent': "Mozilla/5.0",
  38. 'content-type': "application/x-www-form-urlencoded",
  39. 'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  40. 'accept-encoding': "gzip, deflate, br",
  41. 'accept-language': "en-US,en;q=0.8,fr-FR;q=0.6,fr;q=0.4"
  42. }
  43. bin_data = urllib.parse.urlencode(data).encode()
  44. # HTTP Request
  45. targetUrl = "https://portailwifi.ec-nantes.fr/portal_api.php"
  46. status = "NoStatus"
  47. try :
  48. request = urllib.request.Request(targetUrl, bin_data, headers, method="POST")
  49. response = urllib.request.urlopen(request).read().decode()
  50. except :
  51. print("Can't perform an http request against " + targetUrl)
  52. print("Are you connected to Wifi-ECN ?")
  53. else:
  54. pydata = json.loads(response)
  55. try:
  56. status = pydata['type']
  57. except:
  58. try:
  59. status = pydata['error']['code']
  60. print("Wifi ECN sent back en error : " + status)
  61. except:
  62. print("I can't read WifiECN answer. :-(")
  63. # End of programm
  64. if status == "CONNECT":
  65. print("Successfully logged as " + username +". :-)")
  66. else:
  67. print("Something went wrong. Couldn't connect as " + username +".")