Socket address
1.AF_UNIX: string
2.AF_INET: tuple(host,port)
3.AF_INET6:tuple(host,port,flowinfo,scopeid)
Socket 传输类型
1.socket.SOCK_STREAM
2.socket.SOCK_DGRAM
3.socket.SOCK_RAW
协议类型
1.MSG_*
2.SOL_*
3.IPPROTO_*
4.IPPORT_*
5.INADDR_*
6.IP_*
7.IPV6_*
8.EAI_*
9.AI_*
10.NI_*
11.TCP_*
传输层相关常用的函数
1.socket.socket(socket_family,socket_type,prtocol)
2.socket.bind(address)
3.socket.listen(backlog)#backlog是等候队列的数量
4.socket.accept()#return (socket,address)
5.socket.connect(address)
6.socket.connect_ex(address)#出错时返回错误代码
7.socket.recv(bufsize[,flags])
返回tcp数据,bufsize是最大返回值
flags参照[Unix](http://www.sbras.ru/cgi-bin/www/unix_help/unix-man?recv+2)
8.socket.send(string[,flags])#发送tcp数据 return 发送的字节
9.socket.send(string[,flags])#持续的发送string中的数据直到完成或者出现错误,none无错误返回值
10.socket.recvfrom(bufsize[,flags])#从UDP返回数据
11.socket.sendto(string,address)
12.socket.sendto(string,flags,address)#发送UDP数据
其他常用的函数
1.socket.getpeername()#Return the remote address to which the socket is connected.
2.socket.getsockname()#Return the socket’s own address
3.socket.getsockopt(level,optname[,buflen])
4.socket.setsockopt(level,opname,value)#设置socket参数
#文档说see the optional built-in module struct for a way to encode C structures as strings
5.socket.close()
TCP/IP 小例子IP包解析(Python 2.7+Linux)
类型定义
class IP_pk(object):
def __init__(self,list):
self.version=binary_move(list[0],1,4,8)
self.hlen=binary_move(list[0],5,8,8)*4
self.tos=list[1]
self.tolen=list[2]
self.identify=list[3]
self.flags=binary_move(list[4],1,4,16)
self.fragmentoffset=binary_move(list[4],5,16,16)
self.ttl=list[5]
self.protocol=list[6]
self.checksum=list[7]
self.sourip=list[8]
self.destip=list[9]
def show(self):
re = str()
re = re + "Version:" + str(self.version)+"\r\n"
re = re + "HeaderLength:" + str(self.hlen)+"\r\n"
re = re + "totallengh:" + str(self.tolen)+"\r\n"
re = re + "identify:" + str(self.identify)+"\r\n"
re = re + "flags:" + flags_translate(self.flags)+"\r\n"
re = re + "Fragment Offset" + str(self.fragmentoffset)+"\r\n"
re = re + "TTL:" + str(self.ttl)+"\r\n"
re = re + "Protocol:" + protocol_traslate(self.protocol) + "(" + str(self.protocol) + ")"+"\r\n"
re = re + "checksum:" + hex(self.checksum) + "(" + str(self.checksum) + ")"+"\r\n"
re = re + "sourip:" + socket.inet_ntoa(struct.pack("!I", self.sourip))+"\r\n"
re = re + "destip:" + socket.inet_ntoa(struct.pack("!I", self.destip))+"\r\n"
return re
class TCP_header(object):
def __init__(self,list):
self.src=list[0]
self.dst=list[1]
self.sequence=list[2]
self.ack=list[3]
self.dataoffset=binary_move(list[4],1,4,16)*4
self.reserved=binary_move(list[4],5,10,16)
self.control=binary_move(list[4],11,16,16)
self.windowsize=list[5]
self.checksum=list[6]
self.urgentpointer=list[7]
def show(self):
re = str()
re = "Source Port:" + str(self.src) + "\r\n"
re = re + "Destination Port:" + str(self.dst) + "\r\n"
re = re + "Sequece Number:" + str(hex(self.sequence)) + "\r\n"
re = re + "ACK Number:" + str(hex(self.ack)) + "\r\n"
re = re + "Data Offest:" + str(self.dataoffset) + "\r\n"
re = re + "Reserved:" + bin(self.reserved) + "\r\n"
re = re + "Control:" + tcp_control(self.control) + "\r\n"
re = re + "Windows size:" + hex(self.windowsize) + "\r\n"
re = re + "Checksum:" + hex(self.checksum) + "\r\n"
re = re + "Urgent Pointer:"+str(self.urgentpointer)+"\r\n"
return re
class UDP_header(object):
def __init__(self,list):
self.soreport=list[0]
self.destport=list[1]
self.checksum=list[2]
self.tolen=list[3]
def show(self):
re = str()
re="Source Port:"+str(self.soreport)+"\r\n"
re = re + "Dest Port:" + str(self.destport) + "\r\n"
re = re + "Checksum:" + str(self.checksum) + "\r\n"
re = re + "Tolength:" + str(self.tolen)+"\r\n"
return re
解析函数
def tcp_anly(ip_pk,ordata):
re=str()
re= "=====================Translate Protocol=====================\r\n"
#将传入的ordata按照tcp头部解析
tcp_header = TCP_header(struct.unpack("!2H2I4H", ordata[ip_pk.hlen:ip_pk.hlen + 20]))
re = re + tcp_header.show()
#解析tcp选项部分
if tcp_header.dataoffset > 20:
tcp_options_length = tcp_header.dataoffset - 20
start = ip_pk.hlen + 20
one=struct.unpack("!1B",ordata[start:start+1])
start=start+1
while one[0]!=0 :
if one[0]==1:
pass;
if one[0]==2:
two=struct.unpack("!1B",ordata[start:start+1])
start = start + 1
if two==4:
re=re+"MSS:"+str(bin(struct.unpack("!1H",ordata[start:start+2])))+"\r\n"
start=start+2
if one[0] == 3:
two = struct.unpack("!1B", ordata[start:start + 1])
start = start + 1
if two[0] == 3:
re = re + "Window Move Number:" + str("".join(map(bin,struct.unpack("!1B", ordata[start:start + 1])))) + "\r\n"
start = start + 1
if one[0] == 8:
two = struct.unpack("!1B", ordata[start:start + 1])
start = start + 1
if two[0] == 10:
re = re + "Timestamp:" + str(str(struct.unpack("!1I", ordata[start:start + 4])[0])) + "\r\n"
start = start + 4
re = re + "Timestam reply:" + str(str(struct.unpack("!1I", ordata[start:start + 4])[0])) + "\r\n"
start = start +4
#print start
if start>=ip_pk.hlen+tcp_header.dataoffset:
break;
one=struct.unpack("!1B",ordata[start:start+1])
start=start+1
re = re + "=====================Data Part==========================="+"\r\n"
#应用层协议部分
datastart = tcp_header.dataoffset + ip_pk.hlen
if datastart != 0:
datapart = struct.unpack("!" + str(ip_pk.tolen - datastart) + "c", ordata[datastart:ip_pk.tolen])
re = re + "".join(datapart)+"\r\n"
return re
def udp_anly(ip_pk,ordata):
re = str()
re = "=====================Translate Protocol=====================\r\n"
data=struct.unpack("!4H",ordata[ip_pk.hlen:ip_pk.hlen+8])
udp_header=UDP_header(data)
re = re + udp_header.show()
re = re + "=====================Data Part===========================" + "\r\n"
datastart = ip_pk.hlen+8
#print datastart, ip_pk.tolen
if datastart != 0:
datapart = struct.unpack("!" + str(ip_pk.tolen - datastart) + "c", ordata[datastart:ip_pk.tolen])
re = re + "".join(datapart)+"\r\n"
return re
中间函数
def tcpget(logpath="tcplog.txt"):
f=open(logpath,"w")
#通过socket函数创建一个SOCK_RAW,TCP的socket对象
s=socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_TCP)
#报文解析
while True:
ordata=s.recv(65535);
#通过struct包中函数对接收到的数据进行网络-->本机字节的转换
data=struct.unpack("!2B3H2B1H2I",ordata[0:20])
#解析ip首部
ip_pk=IP_pk(data[0:20])
f.write("=====================NetWork Protocol=====================\r\n")
f.write(ip_pk.show())
#解析ip选项字段
ip_options_length=ip_pk.hlen-20
if ip_options_length!=0:
choosepart=struct.unpack("!"+str(ip_options_length)+"c",ordata[20:ip_pk.hlen])
f.write("choosepart:" + "".join(choosepart))
#调用tcp_anly函数对余下的数据进行tcp解析
f.write(tcp_anly(ip_pk,ordata))
f.flush()
f.close()
def udpget(logpath="udplog.txt"):
f = open(logpath, "w")
#通过socket函数创建一个SOCK_RAW,UDP的socket对象
#说明同上
s=socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_UDP)
while True:
ordata,address=s.recvfrom(65535);
print len(ordata)
data=struct.unpack("!2B3H2B1H2I",ordata[0:20])
ip_pk=IP_pk(data[0:20])
f.write("=====================NetWork Protocol=====================\r\n")
f.write(ip_pk.show())
ip_options_length=ip_pk.hlen-20
if ip_options_length!=0:
choosepart=struct.unpack("!"+str(ip_options_length)+"c",ordata[20:ip_pk.hlen])
f.write(" choosepart:"+"".join(choosepart))
f.write(udp_anly(ip_pk,ordata))
f.flush()
f.close()
遇到的问题:
1.Errno 10013:端口占用
2.listen(que_count)不要忘了参数
转载于:https://www.cnblogs.com/Deep1994/p/5571235.html