From ae4a18e70afb05f6a923f118ade4a93ba40ca777 Mon Sep 17 00:00:00 2001 From: zwb <1465302821@qq.com> Date: Mon, 28 Oct 2024 14:20:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 脚本/批量生成JSON文件.txt | 42 +++++++++++++++++++++++++++++++ 脚本/测试端口是否开启(单个).txt | 27 ++++++++++++++++++++ 脚本/测试端口是否开启(批量).txt | 31 +++++++++++++++++++++++ 脚本/生成单个JSON文件.txt | 29 +++++++++++++++++++++ 脚本/读取xslx表里内容.txt | 20 +++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 脚本/批量生成JSON文件.txt create mode 100644 脚本/测试端口是否开启(单个).txt create mode 100644 脚本/测试端口是否开启(批量).txt create mode 100644 脚本/生成单个JSON文件.txt create mode 100644 脚本/读取xslx表里内容.txt diff --git a/脚本/批量生成JSON文件.txt b/脚本/批量生成JSON文件.txt new file mode 100644 index 0000000..c5601ec --- /dev/null +++ b/脚本/批量生成JSON文件.txt @@ -0,0 +1,42 @@ +import json +import os + +# 读取文本文件 +file_path = 'C:/Users/andin/Desktop/2.txt.txt' +output_base_path = r'C:/Users/andin/Desktop/zhuwenbao/1828151960117535477554_{}.json' + +# 逐行读取文本文件 +with open(file_path, 'r', encoding='utf-8') as file: + lines = file.readlines() # 一次性读取所有行 + index = 0 # 用于生成不同的文件名 + + # 每五行为一组 + for i in range(0, len(lines), 5): + group = [] + for j in range(i, min(i + 5, len(lines))): + line = lines[j].strip() + parts = line.split() + if len(parts) >= 4: + ip_address = parts[0] + port = parts[1] + identifier = parts[2] + date_location = ' '.join(parts[3:]) + + data = { + 'ip_address': ip_address, + 'port': int(port), + 'identifier': identifier, + 'date_location': date_location + } + group.append(data) + + # 如果group中有数据,则生成新的文件名并写入JSON文件 + if group: + # 生成新的文件名 + output_json_path = output_base_path.format(index) + # 将当前组的数据写入 JSON 文件 + with open(output_json_path, 'w', encoding='utf-8') as json_file: + json.dump(group, json_file, ensure_ascii=False, indent=4) + + print(f"JSON 文件已成功生成: {output_json_path}") + index += 1 \ No newline at end of file diff --git a/脚本/测试端口是否开启(单个).txt b/脚本/测试端口是否开启(单个).txt new file mode 100644 index 0000000..2eea841 --- /dev/null +++ b/脚本/测试端口是否开启(单个).txt @@ -0,0 +1,27 @@ +import socket + + +def test_port(ip, port): + # 创建一个 socket 对象 + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(5) # 设置超时时间 + + try: + # 尝试连接指定的 IP 和端口 + result = sock.connect_ex((ip, int(port))) # 确保 port 是整数 + if result == 0: + print(f"端口 {port} 在地址 {ip} 上是开放的") + else: + print(f"无法连接到 {ip}:{port}") + except Exception as e: + print(f"连接测试失败: {e}") + finally: + sock.close() # 关闭 socket 连接 + + +if __name__ == "__main__": + # 测试端口,替换下面的 IP 和端口号为你想要测试的值 + test_ip = "121.36.27.6" + target_port =80 + # 这里的 target_port 是一个整数,表示要测试的端口号 + test_port(test_ip, target_port) # 调用函数 test_port \ No newline at end of file diff --git a/脚本/测试端口是否开启(批量).txt b/脚本/测试端口是否开启(批量).txt new file mode 100644 index 0000000..6ba1b82 --- /dev/null +++ b/脚本/测试端口是否开启(批量).txt @@ -0,0 +1,31 @@ +import socket + + +def test_port(ip, port): + # 创建一个 socket 对象 + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(5) # 设置超时时间 + + try: + # 尝试连接指定的 IP 和端口 + result = sock.connect_ex((ip, port)) + if result == 0: + print(f"端口 {port} 在地址 {ip} 上是开放的") + else: + print(f"无法连接到 {ip}:{port}") + except Exception as e: + print(f"连接测试失败: {e}") + finally: + sock.close() # 关闭 socket 连接 + + +if __name__ == "__main__": + filename = 'C:/Users/andin/Desktop/zhuwenbao.txt' + + with open(filename, 'r') as file: + for line in file: + parts = line.strip().split() + if len(parts) >= 2: + ip = parts[0] # 第一部分是 IP 地址 + port = int(parts[1]) # 第二部分是端口号 + test_port(ip, port) # 调用函数 test_port \ No newline at end of file diff --git a/脚本/生成单个JSON文件.txt b/脚本/生成单个JSON文件.txt new file mode 100644 index 0000000..c782aab --- /dev/null +++ b/脚本/生成单个JSON文件.txt @@ -0,0 +1,29 @@ +import json + +# 读取文本文件 +file_path = 'C:/Users/andin/Desktop/wangsu.txt' +output_json_path = 'C:/Users/andin/Desktop/output.json' + +# 初始化一个空列表来存储解析后的数据 +data_list = [] + +# 逐行读取文本文件 +with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + parts = line.strip().split() + if len(parts) >= 3: + number = parts[0] + location_operator = parts[1] + province = parts[2] + data = { + 'number': number, + 'location_operator': location_operator, + 'province': province + } + data_list.append(data) + +# 将数据写入 JSON 文件 +with open(output_json_path, 'w', encoding='utf-8') as json_file: + json.dump(data_list, json_file, ensure_ascii=False, indent=4) + +print(f"JSON 文件已成功生成: {output_json_path}") \ No newline at end of file diff --git a/脚本/读取xslx表里内容.txt b/脚本/读取xslx表里内容.txt new file mode 100644 index 0000000..76cd792 --- /dev/null +++ b/脚本/读取xslx表里内容.txt @@ -0,0 +1,20 @@ +import pandas as pd + +# 确保这里的路径是正确的,并且文件确实存在于此路径下 +file_path = r'C:\Users\andin\Desktop\zhuwenbao.xlsx' + +# 设置 pandas 选项以显示所有行和列 +pd.set_option('display.max_rows', None) # 显示所有行 +pd.set_option('display.max_columns', None) # 显示所有列 + +# 使用 pandas 的 read_excel 函数读取文件 +try: + data = pd.read_excel(file_path) + # 显示数据的全部内容 + print(data) +except Exception as e: + print(f"发生了一个错误: {e}") + + + +