今天看看如何用python实现抓包。

以前我们曾用过winpcap实现抓包,其实今天我们还是用它,只不过是它在python下的模块,在用之前我们必须先下载两个库,pcap库和dpkt库,这个可以在Google

code里很容易找到,需要注意的是pcap只支持到python2.5 ,所以我们要用2.5来编写。

安装完两个库以后,我们就先来认识一下两个库。

首先看pcap。

我们在命令行输入:import

pcap如果回车后没有出错信息,则说明我们安装成功了,然后我们利用help命令来查看一下帮助信息。

模块的说明:This module provides a high level interface to packet capture

systems.All packets on the network, even those destined for other

hosts, areaccessible through this mechanism.

关于它的类,这里只说下最重要的一个pcap类:

class pcap(__builtin__.object)

pcap(name=None, snaplen=65535, promisc=True,

immediate=False, timeout_ms=None) -> packet capture

object。Open a handle to a packet capture descriptor.

name -- name of a network interface or dumpfile to open,

or None to open the first available up interface

snaplen -- maximum number of

bytes to capture for each packet

promisc -- boolean to

specify promiscuous mode sniffing

immediate -- disable buffering, if possible

timeout_ms -- requests for the next packet will return None if

the timeout

(in

milliseconds) is reached and no packets were received

(Default:

no timeout)

我们在一般使用的时候,参数都可省略,如:p=pcap.pcap( )

Pcap的方法中常用的是setfilter()设置过滤,可以使协议,端口。

另外一个库是dpkt库,是用来解析我们抓到的包的,好我们看一个简单的程序。

import pcap

import dpkt

a=pcap.pcap()

a.setfilter('tcp port 80') #过滤功能,可以设置需要显示的

for i,j in a:

tem=dpkt.ethernet.Ethernet(j)

print('%s %x',i,tem)

('%s %x', 1251890284.9400771, Ethernet(src='\x00\x12?\x92\xb0A',

dst='\x00\nB\xcd

off=16384, dst='

ttl=128, id=14698, data=TCP(seq=2887809373L, ack=837333778,

win=65274, sum=38843, flags=17, dport=80, sport=2757))))

('%s %x', 1251890285.1270909,

Ethernet(src='\x00\nB\xcd

dst='\x00\x12?\x92\xb0A', data=IP(src='

dst='\n\x01\x0eP', sum=19969, len=40, p=6, ttl=49, id=14400,

data=TCP(seq=837333778, ack=2887809374L, win=6600, sum=31981,

flags=17, dport=2757, sport=80))))

('%s %x', 1251890285.127131, Ethernet(src='\x00\x12?\x92\xb0A',

dst='\x00\nB\xcd

off=16384, dst='

ttl=128, id=14713, data=TCP(seq=2887809374L, ack=837333779,

win=65274, sum=38842, flags=16, dport=80, sport=2757))))

('%s %x', 1251890285.2843239, Ethernet(src='\x00\x12?\x92\xb0A',

dst='\x00\nB\xcd

off=16384, dst='v\xe4\x94P', sum=40379, len=48, p=6, ttl=128,

id=14727, data=TCP(seq=3061527654L, off_x2=112, sum=27037,

dport=80, sport=2763))))

('%s %x', 1251890285.284482, Ethernet(src='\x00\x12?\x92\xb0A',

dst='\x00\nB\xcd

off=16384, dst='v\xe4\x94\x16', sum=40442, len=40, p=6, ttl=128,

id=14730, data=TCP(seq=4170586011L, ack=215657518, sum=47158,

flags=17, dport=80, sport=2759))))

('%s %x', 1251890285.2853069, Ethernet(src='\x00\x12?\x92\xb0A',

dst='\x00\nB\xcd

off=16384, dst='

ttl=128, id=14740, data=TCP(seq=818783773, off_x2=112, sum=40328,

dport=80, sport=2765))))

这里列出一部分结果。

看到确实抓到啦。

对于结果显示的分析和进一步改进会在今后继续学习的,这里只是开了一个头。

python抓包库_python学习笔记——pcap抓包相关推荐

  1. python爬取基金历史净值_Python学习笔记之抓取某只基金历史净值数据实战案例

    摘要:这篇Python开发技术栏目下的"Python学习笔记之抓取某只基金历史净值数据实战案例",介绍的技术点是"Python学习笔记.Python.历史净值数据.学习笔 ...

  2. python 异步加载_Python学习笔记4——爬取异步加载数据

    一.什么是异步加载? 在之前的学习笔记中,爬取的网页是需要手动翻页的网址,但是一些网站是通过自动加载翻页的,如knewone网页.浏览knewone的官网就能发现,当下拉到网页最下端时,网站会自动加载 ...

  3. python 坐标轴 单位刻度_Python学习笔记(matplotlib篇)--坐标轴刻度

    python学习笔记--坐标轴刻度 所用的库及环境: ide:pycharm python环境:python3.7 matplotlib:   matplotlib 1.11 numpy:  nump ...

  4. python的基本数据结构_Python学习笔记——基本数据结构

    列表list List是python的一个内置动态数组对象,它的基本使用方式如下: shoplist = ['apple', 'mango', 'carrot', 'banana'] print 'I ...

  5. python查看方法作用_python学习笔记1,新手小白也能看得懂

    这是酸菜在风变编程上学习python时积累的学习笔记,希望能帮到同样也在学习中的小伙伴.持续更新~ 第0关 Print()函数 (1)不带引号:让计算机读懂括号里的内容,打印最终的结果 例:print ...

  6. python的基本数据结构_python学习笔记-基本数据结构

    Python 学习笔记-1 写在最前面,因为组内小伙伴要走,生信团队由原来的7个人,慢慢的变的只有我一个人了,需要紧急突击下python,因为有python的流程要交接维护 python 基本情况 代 ...

  7. python创建实例属性_Python学习笔记__6.5章 实例属性和类属性

    # 这是学习廖雪峰老师python教程的学习笔记 1.概览 1.1.实例绑定属性 class Student(object): def __init__(self, name): self.name ...

  8. python __name__怎么使用_python学习笔记26(python中__name__的使用)

    在python中,每个py文件都是一个模块,也都是一个可执行文件,即包含main方法.因此,对每个py文件,可以单独运行,也可以import它给其他客户使用,这两种情况不一样. 1. 如果模块是被导入 ...

  9. python编辑器文字放大_python学习笔记000

    写在最前面 其实想学习编程很久了,以前也陆陆续续学习过一段时间的编程,但总是半途而废,经过我长达三秒钟的思考,总结出以下两个方面的原因: 1.没有学习的压力.因为我不是程序员,编程不是我吃饭的家伙,会 ...

最新文章

  1. 从头学起androidlt;AutoCompleteTextView文章提示文本框.十九.gt;
  2. Reddit欲融资3亿美元,由腾讯领投
  3. 【综述】MV3D-Net、AVOD-Net 用于自动驾驶的多视图3D目标检测网络
  4. get post put delete 区别_GET 和 POST 的区别?
  5. The 15th UESTC Programming Contest Preliminary C - C0ins cdoj1554
  6. 快速设置UITableView不同section对应于不同种类的cell
  7. 【洛谷P1378】油滴扩展
  8. box-sizing属性
  9. 微信公众号Web页面CSS文件里面的样式不加载
  10. 阿里云迎来 2.0,重新定义软件系统应用开发方式
  11. 写给笨人的法线贴图原理 【转】
  12. 解决百度地图使用出现的has leaked ServiceConnection com.baidu.location.b@4aa5a810 that was originally bound here
  13. Windows Server AppFabric 安装文档
  14. 16G kingston U盘 解除写保护
  15. vue无法加载ps1
  16. 若依 后台框架配置丛数据源使用
  17. 史上最全 Vue 前端代码风格指南
  18. 【Win10 C盘压缩卷问题解答】:无法将卷压缩到超出任何不可移动的文件所在点
  19. Scikit Learn-聚类方法
  20. 与其他带货直播平台相比,视频号更适合哪些人?

热门文章

  1. python3.6安装XGB
  2. Google Play/App Store/admob/facebook开发者收款方式有哪些?如何结汇?
  3. 安卓手机odex化与刷机包制作
  4. Form 表单提交的几种方式
  5. C# 3DES加密解密算法
  6. 视频教程-跟着王进老师学开发Python篇:基础入门案例讲解-Python
  7. (转载)前端表格制作教程
  8. python学习之偏函数
  9. SIMCOM推出全网通小尺寸4G模块SIM7100CE
  10. NR 5G新功能-极早寻呼指示(EPI)(R17版本)