python笔记
python启动http服务、打包成exe可执行文件。
1、python打包成可执行文件
pip install pyinstaller
pyinstaller --onefile myscript.py
2、python快速启动一个静态http服务
python -m http.server 8000
参数可以指定文件夹 --directory /path/to/your/folder
import re
import geoip2.database
from itertools import islice
def parse_log_file(log_file, start_line=0):
pattern = r"\[(.*?)\].*?\|\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+\|"
results = []
with open(log_file, "r", encoding="utf-8") as file:
for line in islice(file, start_line, None):
match = re.search(pattern, line)
if match:
timestamp, ip = match.groups()
results.append({"time": timestamp, "ip": ip})
return results
def get_ip_location(ip):
with geoip2.database.Reader("C:\\Users\\admin\\Downloads\\GeoLite2-City.mmdb", locales=["zh-CN"]) as reader:
response = reader.city(ip)
return response.country.name,response.subdivisions.most_specific.name, response.city.name
# ip = "58.34.81.50"
# country, province, city = get_ip_location(ip)
# print(f"IP {ip} 所属地:{country} - {province} - {city}")
log_file = "server-20241206.log"
parsed_data = parse_log_file(log_file)
for entry in parsed_data:
country, province, city = get_ip_location(entry['ip'])
print(f"时间: {entry['time']}, IP: {entry['ip']}, 所属地: {country} - {province} - {city}")