Qilan2 commited on
Commit
f78a28e
·
verified ·
1 Parent(s): 53a480e

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +27 -9
server.py CHANGED
@@ -383,14 +383,23 @@ def get_current_ip():
383
 
384
  def get_geo_info(ip_address):
385
  """根据 IP 地址获取地理位置信息"""
386
- url = f"https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip_address}"
 
 
 
387
 
388
- response = requests.get(url)
389
 
390
  if response.status_code == 200:
391
- geo_info = response.json()
392
- if geo_info['code'] == 'Success':
393
- return geo_info['data']
 
 
 
 
 
 
394
  else:
395
  print("无法获取地理位置信息")
396
  return None
@@ -402,29 +411,38 @@ def replace_ip_and_country_in_file(file_path):
402
  try:
403
  # 获取当前 IP 地址
404
  ip_address = get_current_ip()
 
405
 
406
  # 获取地理位置信息
407
  geo_info = get_geo_info(ip_address)
408
 
409
  if geo_info is not None:
 
 
410
  # 读取文件内容
411
  with open(file_path, 'r', encoding='utf-8') as file:
412
  content = file.read()
 
413
 
414
- # 使用正则表达式替换方括号中的内容
415
- new_content = re.sub(r'\[ip\]', f'[{ip_address}]', content)
416
- new_content = re.sub(r'\[country\]', f'[{geo_info["country"]}]', new_content)
 
 
 
417
 
418
  # 将修改后的内容写回文件
419
  with open(file_path, 'w', encoding='utf-8') as file:
420
  file.write(new_content)
421
 
422
- print(f"成功将 [ip] 更新为 '[{ip_address}]', [country] 更新为 '[{geo_info['country']}]'")
423
 
424
  except FileNotFoundError:
425
  print(f"错误: 文件 {file_path} 不存在。")
426
  except IOError as e:
427
  print(f"IO 错误: {e}")
 
 
428
 
429
 
430
 
 
383
 
384
  def get_geo_info(ip_address):
385
  """根据 IP 地址获取地理位置信息"""
386
+ url = f"https://api.qjqq.cn/api/Local"
387
+ headers = {
388
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
389
+ }
390
 
391
+ response = requests.get(url, headers=headers)
392
 
393
  if response.status_code == 200:
394
+ geo_info = response.json() # 解析为 JSON 格式
395
+
396
+ if geo_info['code'] == 200:
397
+ data = geo_info['data']
398
+ return {
399
+ 'ip': data['ip'],
400
+ 'country': data['country'],
401
+ 'isp': data['isp']
402
+ }
403
  else:
404
  print("无法获取地理位置信息")
405
  return None
 
411
  try:
412
  # 获取当前 IP 地址
413
  ip_address = get_current_ip()
414
+ print(f"获取到当前 IP: {ip_address}")
415
 
416
  # 获取地理位置信息
417
  geo_info = get_geo_info(ip_address)
418
 
419
  if geo_info is not None:
420
+ print(f"获取到地理信息: {geo_info}")
421
+
422
  # 读取文件内容
423
  with open(file_path, 'r', encoding='utf-8') as file:
424
  content = file.read()
425
+ # print(f"读取到的文件内容: {content}")
426
 
427
+ # 使用正则表达式匹配 "IP[xxx]国家[yyy]" 的格式
428
+ pattern = r'IP\[.*?\]国家\[.*?\]'
429
+ replacement = f'机子IP[{geo_info["ip"]}]国家[{geo_info["country"]}] ISP[{geo_info["isp"]}]'
430
+ new_content = re.sub(pattern, replacement, content)
431
+
432
+ # print(f"替换后的内容: {new_content}")
433
 
434
  # 将修改后的内容写回文件
435
  with open(file_path, 'w', encoding='utf-8') as file:
436
  file.write(new_content)
437
 
438
+ print(f"成功更新为: IP[{geo_info['ip']}] 国家[{geo_info['country']}] ISP[{geo_info['isp']}]")
439
 
440
  except FileNotFoundError:
441
  print(f"错误: 文件 {file_path} 不存在。")
442
  except IOError as e:
443
  print(f"IO 错误: {e}")
444
+ except Exception as e:
445
+ print(f"发生其他错误: {e}")
446
 
447
 
448