1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| import socket import requests import subprocess import re import json from urllib.parse import urlencode from urllib.parse import parse_qs, urlparse
def get_host_ip(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("10.255.255.255", 1)) ip = s.getsockname()[0] finally: s.close() return ip
user_ip = get_host_ip() print("局域网ip为{}".format(user_ip))
user_name = "your username" user_password = "your password"
def LogIn(): login_url = "http://202.38.64.59/cgi-bin/ip" go_login = str("登录账户".encode("GBK")) go_login = go_login[2:-1] login_data = { "cmd": "login", "url": "URL", "ip": user_ip, "name": user_name, "password": user_password, "go": go_login } login_header = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9", "Cache-Control": "max-age=0", "Connection": "keep-alive", "Content-Length": str(len(urlencode_simple(login_data))), "Content-Type": "application/x-www-form-urlencoded", "Cookie": "name=; password=", "Host": "202.38.64.59", "Origin": "http://202.38.64.59", "Referer": "http://202.38.64.59/cgi-bin/ip", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36" } rq = requests.post(login_url, headers=login_header, data=login_data) if "Set-Cookie" in rq.headers: cookie_rn = rq.headers["Set-Cookie"] else : print("No cookie in headers! Headers:\n{}\nText:\n{}".format(rq.headers, rq.text)) cookie_rn = "" return rq.status_code, cookie_rn
def Connect(cookie_rn): connect_url = r"http://202.38.64.59/cgi-bin/ip?cmd=set&url=URL&type=8&exp=0&go=+%BF%AA%CD%A8%CD%F8%C2%E7+" connect_headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Cookie': 'name=; password=; {rn}'.format(rn = cookie_rn), 'Host': '202.38.64.59', 'Referer': 'http://202.38.64.59/cgi-bin/ip', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' } go_submit = " 开通网络 " query_string_params = { 'cmd': 'set', 'url': 'URL', 'type': '8', 'exp': '0', "go": go_submit.encode("GBK") } rq = requests.get(connect_url, headers=connect_headers, data=query_string_params) return rq.status_code
def LogOut(cookie_rn): try: cookie_rn = cookie_rn except : raise ValueError("Please Login() before Logout()!") logout_url = "http://202.38.64.59/cgi-bin/ip?cmd=logout" logout_headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Cookie': 'name=; password=; {rn}'.format(rn = cookie_rn), 'Host': '202.38.64.59', 'Referer': 'http://202.38.64.59/cgi-bin/ip?cmd=set&url=URL&type=8&exp=0&go=+%BF%AA%CD%A8%CD%F8%C2%E7+', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', } logout_data = {"cmd": "logout"} rq = requests.get(logout_url, headers = logout_headers, data = logout_data) if rq.status_code != 200: print("In _logout(): \n logout failed with status code {sc}, \n headers:\n{hd} \n text:\n{tx}".\ format(sc = rq.status_code, hd = rq.headers, tx = rq.text)) ret = rq.status_code rq.close() return ret
def Ping(): backinfo = subprocess.call("ping www.bilibili.com -n 1", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if backinfo: print("网络未连接") return 0 else: print("网络已连接") return 1
def urlencode_simple(query: dict): url = "" for key in query: keycpy = str(key) value = str(query[key]) if type(query[key]) == bytes: value = value[2:-1] keycpy = keycpy.replace('@', '%40') keycpy = keycpy.replace(r'\x', '%') keycpy = keycpy.replace(r' ', '+') value = value.replace('@', '%40') value = value.replace(r'\x', '%') value = value.replace(r' ', '+') url += "{}={}&".format(keycpy, value) url = url[0:-1] return url
if __name__ == "__main__":
connection = Ping()
print("连接网络中·····")
status_code, cookie_rn = LogIn() if status_code != 200: print("LogIn Error with status code: {}".format(status_code)) exit() else: print("Login Response code: {}".format(status_code))
status_code = Connect(cookie_rn) if status_code != 200: print("Connect Error with status code: {}".format(status_code)) exit() else: print("Connect Response code: {}".format(status_code))
connection = Ping()
status_code = LogOut(cookie_rn) if status_code != 200: print("LogOut Error with status code: {}".format(status_code)) exit() else: print("LogOut Response code: {}".format(status_code))
connection = Ping()
exit()
|