Python Raw Socket使用示例(发送TCP SYN数据包)
说实话,Python真的不太适合做这种二进制的东西,天生没有指针,导致在C/C++很容易的东西在Python下就很麻烦。不过好像3.1有了原生的bytes类型,不知道能不能改变现状。
import sys
import time
import socket
import struct
import random
def SendPacketData (Buffer = None , DestIP = "127.0.0.1" , DestPort = 0) :
"""SendPacketData"""
if Buffer is None :
return False
try:
Socket = socket.socket(socket.AF_INET,socket.SOCK_RAW)
Socket.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)
Socket.setsockopt(socket.SOL_SOCKET,socket.SO_SNDTIMEO,2000)
except:
Socket.close()
return False
try:
Socket.sendto(Buffer,0,(DestIP , DestPort))
except:
Socket.close()
return True
def SetPacketAddress (Number = 1) :
"""SetPakcetAddress"""
return [".".join(["%d" % random.randint(1 , 0xFF) for i in range(0,4)]) for n in range(0 , Number)]
def SetPacketData (Length = 32) :
"""SetPacketData"""
return "".join(["%s" % chr(random.randint(0 , 255)) for n in range(Length)])
def SetPacketCheckSum (Buffer = None) :
"""SetPacketCheckSum"""
if Buffer == None :
return False
if len(Buffer) % 2 :
Buffer += '\0'
return ~sum([(ord(Buffer[n + 1]) << 8) + ord(Buffer[n]) for n in range(0 , len(Buffer) , 2)])
#or return ~sum([ord(Buffer[n]) + ord(Buffer[n + 1]) * 256 for n in range(0 , len(Buffer) , 2)])
def SynSendInit (DestIP = "127.0.0.1" , DestPort = 80) :
"""SynSendInit"""
IpHdrLen = 20
TcpHdrLen = 20
IpVerlen = (4 << 4 | IpHdrLen / 4)
IpTotal_len = socket.htons(IpHdrLen + TcpHdrLen)
IpDestIP = struct.unpack('i',socket.inet_aton(DestIP))[0]
IpSourceIP = struct.unpack('i',socket.inet_aton(SetPacketAddress()[0]))[0]
TcpSport = socket.htons(random.randint(1024 , 65000))
TcpDport = socket.htons(DestP
相关文档:
源代码下载:下载地址在这里
# 022
listNum1 = [1, 3]
listNum2 = [2, 4]
listStr1 = ['a', 'c']
listStr2 = ['b', 'd']
# 列表的合并
list1 = listNum1 + listStr1
for ele in list1:
print ele
print '\n'
# 判断列表中是否包含某元素
print 'b' in list1
print 1 in list1
# 删除某个元素
for ele in ......
源代码下载:下载地址在这里
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# 也可以使用整型作为唯一的编号
dict2 = {
5064001:'Mememe',
506400 ......
源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()
output:
>>>
This is the first line.
This is the second line.
This is ......
源代码下载:下载地址在这里
# 033
class Person:
age = 22
def sayHello(self): # 方法与函数的区别在于需要一个self参数
print 'Hello!'
# end of def
# end of class # 这是我良好的编程风格
p = Person()
print p.age
p.sayHello()
output:
22
Hello! ......
首先是下载python3,现在的最高版本是3.1.1
for linux。
我的放置路径是/home/python下放置Python-3.1.1.tgz,执行以下系列操作:
1.解压:tar zxvf Python-3.1.1.tgz----生成解压包Python-3.1.1
2.转换到Python-3.1.1路径下,执行./configure
3.make
4.make install
在rehl5中已经默认安装了python2.4,所以要做如下 ......