Python 我能';看不出是什么触发了无效语法错误
我目前正在尝试用python复制一个包嗅探器,它可以解析udp、tcp和ICPM包。 现在,除了一个无效的语法错误外,没有其他编译器错误显示Python 我能';看不出是什么触发了无效语法错误,python,syntax-error,Python,Syntax Error,我目前正在尝试用python复制一个包嗅探器,它可以解析udp、tcp和ICPM包。 现在,除了一个无效的语法错误外,没有其他编译器错误显示 #packet sniffer for linux in python import socket, sys from struct import* #A function that converts a string of 6 characters of ethernet address into dash seperated hex string
#packet sniffer for linux in python
import socket, sys
from struct import*
#A function that converts a string of 6 characters of ethernet address into dash seperated hex string
def ethernet_address (string):
new_String = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(string(0)), ord(string(1)), ord(string(2)), ord(string(3)), ord(string(4)), ord(string(5)))
return new_String
#Section that creates the socket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.ntohs(0x0003))
except socket.error as msg:
print ("Socket could not be created, Error : ")
print (msg)
sys.exit()
#recieve a packet
while True:
packet = sock.recvfrom(60000)
#packet string from tuple
packet = packet[0]
#parse ethernet header
ether_length = 14
ether_Header = packet[:ether_length]
ether = unpack('!6s6sh', ether_Header)
ether_protocol = socket.ntohs(ether[2])
print ("Destination MAC: " + ethernet_address(packet[0:6]) + "Source MAC: " + ethernet_address(packet[6:12]) + " Protocol: " + str(ether_protocol))
#This section handles parsing IP packets
if ether_protocol == 8:
#Parse the IP header
#take the first 20 character from the header
ip_header = packet[ether_length:20 + ether_length]
#now unpack
iph = unpack('!BBHHHBBH4s4s', ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xf
iph_length = ihl * 4
ttl = iph[5]
protocol = iph[6]
source_address = socket.inet_ntoa( iph[8] )
destination_address = socket.inet_ntoa( iph[9] )
print("Version: " + str(version) + " IP header length: " + str(ihl) + " TTL: " + str(ttl) + " Protocol: " + str(protocol + " Source Address: " + str(source_address) + " Destination Address: " + str(destination_address) )
#This section handles parsing TCP packets
if protocol == 6 : #This is the line with the error******************************************
num = iph_length + ether_length
tcp_header = packet[ num:num+20]
#unpack
tcph = unpack('!HHLLBBHHH', tcp_header)
source_port = tcph[0]
destination_port = tcph[1]
sequence = tcph[2]
acknowledgment = tcph[3]
doff_reserve = tcph[4]
tcph_length = doff_reserve >> 4
print("Source Port: " + str(source_port) + " Destination Port: " + destination_port + " Sequence: " + sequence + " Acknowledgment: " + str(acknowledgment) + " TCP header length: " + str(tcph_length))
h_size = ether_length + iph_length + tcph_length * 4
data_size = len(packet) - h_size
#get data
data = packet[h_size:]
print ("Data: "+ data)
#This section Handles parsing ICMP packets
elif protocol == 1:
u = iph_length + ether_length
icmph_length = 4
icmp_header = packet[u:u+20]
#unpack
icmph = unpack('!BBH', icmp_header)
icmp_type = icmph[0]
code = icmph[1]
checksum = icmph[2]
print("Type: " + str(icmp_type) + " Code: " + str(code) + " Checksum: " + str(checksum))
h_size = ether_length + iph_length + icmph_length
data_size = len(packet) - h_size
#retrive data
data = packet[h_size]
print("Data: " + data)
#This section handles parsing UDP
elif protocol == 17:
u = iph_length +ether_length
udph_length = 8
udp_header = packet[u:u+8]
#now unpack them
udph = unpack('!HHHH', udp_header)
src_port = udph[0]
dest_port = udph[1]
length = udph[2]
checksum = udph[3]
print("Source Port: " + str(src_port) + " Destination Port: " + str(dest_port) + " Length: " + str(length) + " Checksum: " + str(checksum) )
h_size = ether_length + iph_length + udph_length
data_size = len(packet) - h_size
#get data
data = packet[h_size]
print("Data: " + data)
else:
print("Protocol is something other than UDP, ICMP, or TCP")
print
错误是由以下行引起的:
if protocol == 6 :
VisualStudio说这是一个语法错误,但我不清楚它是如何编写的,因为它与其他所有if语句一样,并且它们没有语法错误警告。如果有错误,会有什么错误?我认为您缺少一个括号<代码>打印(“版本:“+str(版本)+”IP头长度:“+str(ihl)+”TTL:“+str(TTL)+”协议:“+str(协议+”源地址:“+str(源地址)+”目的地址:“+str(目的地址))”)应该可以工作
注意:我在末尾添加了一个括号,但尽管删除了错误,但这可能不是代码按预期运行时应该添加括号的地方。似乎
print(“Version:+str(Version)+”IP头长度:“+str(ihl)+”TTL:“+str(TTL)+”协议:“+str(协议)+”源地址:“+str(源地址)+”目的地址:“+str(目的地址))
很可能是您想要的。我相信您缺少一个括号<代码>打印(“版本:“+str(版本)+”IP头长度:“+str(ihl)+”TTL:“+str(TTL)+”协议:“+str(协议+”源地址:“+str(源地址)+”目的地址:“+str(目的地址))”)应该可以工作
注意:我在末尾添加了一个括号,但尽管删除了错误,但这可能不是代码按预期运行时应该添加括号的地方。似乎
print(“Version:+str(Version)+”IP头长度:“+str(ihl)+”TTL:“+str(TTL)+”协议:“+str(协议)+”源地址:“+str(源地址)+”目的地址:“+str(目的地址))
可能是您想要的。前一行有一个不匹配的括号:
print("Version: " +
str(version) +
" IP header length: " +
str(ihl) +
" TTL: " +
str(ttl) +
" Protocol: " +
str(protocol +
" Source Address: " +
str(source_address) +
" Destination Address: " +
str(destination_address)
)
你可能想要这样的东西:
打印(“版本:”+
str(版本)+
“IP头长度:”+
str(国际人道主义法)+
“TTL:”+
str(ttl)+
“协议:”+
str(协议)+
“源地址:”+
str(源地址)+
“目的地址:”+
str(目的地地址)
)
也可以考虑使用<代码>“.Cube(迭代)以空格加入<代码> STR < /代码> s。连接效率低(此处不是问题)且难以阅读(此处绝对是问题)。
前一行中有一个不匹配的括号:print("Version: " +
str(version) +
" IP header length: " +
str(ihl) +
" TTL: " +
str(ttl) +
" Protocol: " +
str(protocol +
" Source Address: " +
str(source_address) +
" Destination Address: " +
str(destination_address)
)
你可能想要这样的东西:
打印(“版本:”+
str(版本)+
“IP头长度:”+
str(国际人道主义法)+
“TTL:”+
str(ttl)+
“协议:”+
str(协议)+
“源地址:”+
str(源地址)+
“目的地址:”+
str(目的地地址)
)
也可以考虑使用<代码>“.Cube(迭代)以空格加入<代码> STR < /代码> s。连接效率低(此处不是问题)且难以读取(此处肯定是问题)。
错误出现在上面的一行:print(“版本:”+str(Version)+“IP头长度:”+str(ihl)+“TTL:”+str(TTL)+“协议:”+str(协议+”源地址:“+str(源地址)+”目的地址:“+str(目的地址))
具体来说,错误来自
…str(协议+…
,应该是…str(协议)+…
在上面的行中可以找到错误:
print(“版本:”+str(Version)+“IP头长度:”+str(ihl)+“TTL:”+str(TTL)+“协议:”+str(协议+”源地址:“+str(源地址)+”目的地址:“+str(目的地址))
具体来说,错误来自
…str(协议+…
,应该是…str(协议)+…
您计算过前一行中的开始括号和结束括号的数量吗?请记住,当一个语句缺少括号时,它在读取更多行之前无法诊断错误。这就是为什么它指向if
。您计算过前一行中的开始括号和结束括号的数量吗?请记住,当语句缺少括号时,只有读取更多行才能诊断错误。这就是为什么它指向if
。