gpt4 book ai didi

python - 如何在文本文件的不同行中写入每个 IP 地址?

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:53 27 4
gpt4 key购买 nike

from scapy.all import *


pkts = rdpcap("lalalao.pcap")

for p in pkts:
## print p.time


if IP in p: #if packet has IP layer
src_ip = p[IP].src
dest_ip = p[IP].dst
print src_ip

f = open('IP_src.txt', 'a+')
for ip in src_ip:
f.writelines(ip)

f.close()

如何将每个ip写在文本文件的不同行中?代码无效!

最佳答案

writelines不添加换行符。

Write a list of lines to the stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.


使用print statement (print function 在 Python 3.x 中)在字符串末尾写换行符:

with open('IP_src.txt', 'a+') as f:
for ip in src_ip:
print >>f, ip # Python 2.x
# print(ip, file=f) # Python 3.x

或手动添加行:

with open('IP_src.txt', 'a+') as f:
for ip in src_ip:
f.write(ip + '\n')

关于python - 如何在文本文件的不同行中写入每个 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42604788/

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