gpt4 book ai didi

python - 文本文件变成了一些胡言乱语

转载 作者:行者123 更新时间:2023-12-04 08:22:23 28 4
gpt4 key购买 nike

这是原来在pcap中的tcpdump,改成txt。
输入:- sip.txt

Ôò¡          Ü     ªkã_¹¦ R  R   hIÿkRT 4V E`D]  @9°Ã'ö%1æËÄÞ÷0ûðSIP/2.0 403 Forbidden
Via: SIP/2.0/UDP XXX.XX.XX.X:57079;branch=94tsjam66cmay5bpswyfta0nebw34zhfctjuuge2baevikbk03opf15t6wvovnb82mjih3v;received=IP;rport=57079
From: "IP" <sip:IP@IP>;tag=0c26cd11
To: <sip:XXXXXX@XXX.XX.XX.XXX>;tag=as3a5a21bf
Call-ID: 88c218486f66a6aa214d483d988dfa9c
CSeq: 2 INVITE
Server: Asterisk
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, PRACK, MESSAGE
Supported: replaces, timer
Reason: Q.850;cause=21
Content-Length: 0
我的代码
import re
from collections import defaultdict
import io

with io.open('file location','rb',encoding='utf-8') as f:

text = f.readlines()
result = []
blocks = text.split('\n\n\n')

# print(blocks)

print(len(blocks))
IP_add_dict_list = defaultdict(list)
IP_add_dict_set = defaultdict(set)

for block in blocks:
if ("CSeq: 1 INVITE" or "CSeq: 1 INVITE") in block:
caller = r";received=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3};"
x = re.search(caller, block)
callerIP = x.group()
callerIP = callerIP[10:-1]
to = r"To: <sip:(\d|[+]|(0-9))*@"
y = re.search(to, block)
toSip = y.group()
toSip = toSip[9:-1]
call = (callerIP, toSip)
result.append(call)
# print(callerIP, toSip)
IP_add_dict_list[callerIP].append(toSip)
IP_add_dict_set[callerIP].add(toSip)
print(IP_add_dict_list)
print("---------------")
print(IP_add_dict_set)

我的输出:-错误
with io.open('file location', 'rb', encoding='utf-8') as f:
ValueError: binary mode doesn't take an encoding argument

最佳答案

你在评论中说你用来保存输出的命令是

tcpdump -i eth0 -s 1500 udp port 5060 -w sip.txt
tcpdump -w以描述的二进制格式保存捕获的数据包,例如 herehere .文件名对此没有影响。 tcpdump man page中的描述说:

-w
Write the raw packets to file rather than parsing and printing them out.


它是 -w选项未设置,tcpdump 而是解析数据包并以文本形式打印出来。如果要保存此输出,只需将输出重定向到文件,例如与 tcpdump -l ... > dump.txttcpdump -l ... >> dump.txt ,其中 -l options 告诉它缓冲 stdout 行,确保即使 tcpdump 输出也被实际写入被终止,例如Ctrl-C。
同时使用 -w并且输出重定向可能不会做太多,就像 -w ,标准输出上几乎没有输出。

关于python - 文本文件变成了一些胡言乱语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65440206/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com