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
相关文档:
例1:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 019
# 使用“import”语句调用模块:
import _018_Module
_ ......
源代码下载:下载地址在这里
# 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 ......
源代码下载:下载地址在这里
# 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 ......
源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as sala ......
可以播放大部分的音视频.
demo download: http://www.sandy1219.com/python/media.rar
playMP3.py
# -*- coding: utf-8 -*-
import wx;
import wx.media;
import os;
import SPrint;
import mediaStateBar;
import mediaList;
import SaveLog;
import MediaItem;
woldcart = "media files|*.*|avi ......