導航:首頁 > 編程語言 > scapyallpython

scapyallpython

發布時間:2023-03-20 09:41:55

❶ 為什麼在python中import scapy報錯

問題原因:import scapy後執行腳本調用scapy模塊中(默認自動加了.py後綴)優先找了當前目錄的man.py,因為兩文件不一樣(一個是我們引入別人寫好的庫文件,一個是我們自己創建的文件),所以就報錯了。

一、如果一個錯誤出現後沒有被捕獲(捕獲是什麼先不管,現在就理解為出現了一個錯誤),它就一直被往上拋,最終將被Python解釋器捕獲。然後就在本該輸出結果的地方列印一大串錯誤信息,然後程序退出。示例代碼如下:

❷ 如何python 中運行scapy shell

啟用shell
可以使用如下命令啟用shell
[python] view plain
scrapy shell <url>
其中<url>就是你想抓取的頁面url

使用shell
Scrapy shell可以看成是一個內置了幾個有用的功能函數的python控制台程序。

功能函數
shelp() - 輸出一系列可用的對象和函數
fetch(request_or_url)-從給定的url或既有的request請求對象重新生成response對象,並更新原有的相關對象
view(response)-使用瀏覽器打開原有的response對象(換句話說就是html頁面)
Scrapy 對象
使用Scrapy shell下載指定頁面的時候,會生成一些可用的對象,比如Response對象和Selector對象(Html和XML均適用)
這些可用的對象有:

crawler - 當前的Crawler對象
spider
request - 最後獲取頁面的請求對象
response - 一個包含最後獲取頁面的響應對象
sel - 最新下載頁面的Selector對象
settings - 當前的Scrapy settings
Scrapy shell例子
以我的個人博客作為測試:http://blog.csdn.net/php_fly
首先,我們啟動shell
[python] view plain
scrapy shell http://blog.csdn.net/php_fly --nolog
以上命令執行後,會使用Scrapy downloader下載指定url的頁面數據,並且列印出可用的對象和函數列表

[python] view plain
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x0000000002AEF7B8>
[s] item {}
[s] request <GET http://blog.csdn.net/php_fly>
[s] response <200 http://blog.csdn.net/php_fly>
[s] sel <Selector xpath=None data=u'<html xmlns="http://www.w3.org/1999/xhtm'>
[s] settings <CrawlerSettings mole=None>
[s] spider <Spider 'default' at 0x4cdb940>
[s] Useful shortcuts:
[s] shelp() Shell help (print this help)
[s] fetch(req_or_url) Fetch request (or URL) and update local objects
[s] view(response) View response in a browser
獲取曾是土木人博客的文章列表超鏈接
[python] view plain
In [9]: sel.xpath("//span[@class='link_title']/a/@href").extract()
Out[9]:
[u'/php_fly/article/details/19364913',
u'/php_fly/article/details/18155421',
u'/php_fly/article/details/17629021',
u'/php_fly/article/details/17619689',
u'/php_fly/article/details/17386163',
u'/php_fly/article/details/17266889',
u'/php_fly/article/details/17172381',
u'/php_fly/article/details/17171985',
u'/php_fly/article/details/17145295',
u'/php_fly/article/details/17122961',
u'/php_fly/article/details/17117891',
u'/php_fly/article/details/14533681',
u'/php_fly/article/details/13162011',
u'/php_fly/article/details/12658277',
u'/php_fly/article/details/12528391',
u'/php_fly/article/details/12421473',
u'/php_fly/article/details/12319943',
u'/php_fly/article/details/12293587',
u'/php_fly/article/details/12293381',
u'/php_fly/article/details/12289803']
修改scrapy shell的請求方式:

[python] view plain
>>> request = request.replace(method="POST")
>>> fetch(request)
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x1e16b50>
...

從Spider中調用Scrapy shell
在爬蟲運行過程中,有時需要檢查某個響應是否是你所期望的。
這個需求可以通過scrapy.shell.inspect_response函數進行實現
以下是一個關於如何從spider中調用scrapy shell的例子

[python] view plain
from scrapy.spider import Spider

class MySpider(Spider):
name = "myspider"
start_urls = [
"http://example.com",
"http://example.org",
"http://example.net",
]

def parse(self, response):
# We want to inspect one specific response.
if ".org" in response.url:
from scrapy.shell import inspect_response
inspect_response(response)

# Rest of parsing code.
當你啟動爬蟲的時候,控制台將列印出類似如下的信息
[python] view plain
2014-02-20 17:48:31-0400 [myspider] DEBUG: Crawled (200) <GET http://example.com> (referer: None)
2014-02-20 17:48:31-0400 [myspider] DEBUG: Crawled (200) <GET http://example.org> (referer: None)
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x1e16b50>
...
>>> response.url
'http://example.org'
注意:當Scrapy engine被scrapy shell佔用的時候,Scrapy shell中的fetch函數是無法使用的。 然而,當你退出Scrapy shell的時候,蜘蛛將從停止的地方繼續爬行

❸ Python中scapy和socket性能優化的問題

我想不出還有什麼更好解決辦法了。期待高手解答。

❹ python3.2 下的抓包庫。。無論是pypcap還是scapy。貌似都沒有py3的版本。。跪求一個可以python3用

有一個py3kcap是pycap的封裝版本,可以用於python3版本。

給你一個使用的示例代碼:

#!/usr/bin/env python3.2
import ctypes,sys
from ctypes.util import find_library
#pcap = ctypes.cdll.LoadLibrary("libpcap.so")
pcap = None
if(find_library("libpcap") == None):
print("We are here!")
pcap = ctypes.cdll.LoadLibrary("libpcap.so")
else:
pcap = ctypes.cdll.LoadLibrary(find_library("libpcap"))
# required so we can access bpf_program->bf_insns
"""
struct bpf_program {
u_int bf_len;
struct bpf_insn *bf_insns;}
"""
class bpf_program(ctypes.Structure):
_fields_ = [("bf_len", ctypes.c_int),("bf_insns", ctypes.c_void_p)]
class sockaddr(ctypes.Structure):
_fields_=[("sa_family",ctypes.c_uint16),("sa_data",ctypes.c_char*14)]
class pcap_pkthdr(ctypes.Structure):
_fields_ = [("tv_sec", ctypes.c_long), ("tv_usec", ctypes.c_long), ("caplen", ctypes.c_uint), ("len", ctypes.c_uint)]

pkthdr = pcap_pkthdr()
program = bpf_program()
# prepare args
snaplen = ctypes.c_int(1500)
#buf = ctypes.c_char_p(filter)
optimize = ctypes.c_int(1)
mask = ctypes.c_uint()
net = ctypes.c_uint()
to_ms = ctypes.c_int(100000)
promisc = ctypes.c_int(1)
filter = bytes(str("port 80"), 'ascii')
buf = ctypes.c_char_p(filter)
errbuf = ctypes.create_string_buffer(256)
pcap_close = pcap.pcap_close
pcap_lookupdev = pcap.pcap_lookupdev
pcap_lookupdev.restype = ctypes.c_char_p
#pcap_lookupnet(dev, &net, &mask, errbuf)
pcap_lookupnet = pcap.pcap_lookupnet
#pcap_t *pcap_open_live(const char *device, int snaplen,int promisc, int to_ms,
#char *errbuf
pcap_open_live = pcap.pcap_open_live
#int pcap_compile(pcap_t *p, struct bpf_program *fp,const char *str, int optimize,
#bpf_u_int32 netmask)
pcap_compile = pcap.pcap_compile
#int pcap_setfilter(pcap_t *p, struct bpf_program *fp);
pcap_setfilter = pcap.pcap_setfilter
#const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h);
pcap_next = pcap.pcap_next
# int pcap_compile_nopcap(int snaplen, int linktype, struct bpf_program *program,
# const char *buf, int optimize, bpf_u_int32 mask);
pcap_geterr = pcap.pcap_geterr
pcap_geterr.restype = ctypes.c_char_p
#check for default lookup device
dev = pcap_lookupdev(errbuf)
#override it for now ..
dev = bytes(str("wlan0"), 'ascii')
if(dev):
print("{0} is the default interface".format(dev))
else:
print("Was not able to find default interface")

if(pcap_lookupnet(dev,ctypes.byref(net),ctypes.byref(mask),errbuf) == -1):
print("Error could not get netmask for device {0}".format(errbuf))
sys.exit(0)
else:
print("Got Required netmask")
handle = pcap_open_live(dev,snaplen,promisc,to_ms,errbuf)
if(handle is False):
print("Error unable to open session : {0}".format(errbuf.value))
sys.exit(0)
else:
print("Pcap open live worked!")
if(pcap_compile(handle,ctypes.byref(program),buf,optimize,mask) == -1):
# this requires we call pcap_geterr() to get the error
err = pcap_geterr(handle)
print("Error could not compile bpf filter because {0}".format(err))
else:
print("Filter Compiled!")
if(pcap_setfilter(handle,ctypes.byref(program)) == -1):
print("Error couldn't install filter {0}".format(errbuf.value))
sys.exit(0)
else:
print("Filter installed!")
if(pcap_next(handle,ctypes.byref(pkthdr)) == -1):
err = pcap_geterr(handle)
print("ERROR pcap_next: {0}".format(err))
print("Got {0} bytes of data".format(pkthdr.len))
pcap_close(handle)

❺ python程序分析pcap文件的丟包率問題,

使用scapy、scapy_http就可以方便的對pcap包中的http數據包進行解析

#!/usr/bin/env python
try:
import scapy.all as scapy
except ImportError:
import scapy

try:
# This import works from the project directory
import scapy_http.http
except ImportError:
# If you installed this package via pip, you just need to execute this
from scapy.layers import http

packets = scapy.rdpcap('f:\\abc123.pcap')
for p in packets:
print '=' * 78
[python] view plain
#print p.show()
for f in p.payload.fields_desc:
if f.name == 'src' or f.name == 'dst':
ct = scapy.conf.color_theme
vcol = ct.field_value
fvalue = p.payload.getfieldval(f.name)
reprval = f.i2repr(p.payload,fvalue)
print "%s : %s" % (f.name, reprval)

for f in p.payload.payload.fields_desc:
if f.name == 'load':
ct = scapy.conf.color_theme
vcol = ct.field_value
fvalue = p.payload.getfieldval(f.name)
reprval = f.i2repr(p.payload,fvalue)
print "%s : %s" % (f.name, reprval)

其中,p為數據包,scapy_http將其分為:
Ethernet->TCP->RAW三個層次,

使用p.show()函數可以列印出如下結果:
###[ Ethernet ]###
dst = 02:00:00:00:00:39
src = 00:00:00:01:02:09
type = 0x800
###[ IP ]###
version = 4L
ihl = 5L
tos = 0x0
len = 1014
id = 7180
flags =
frag = 0L
ttl = 45
proto = tcp
chksum = 0xbbf9
src = 126.209.59.13
dst = 121.113.176.25
\options \
###[ Raw ]###
load = '.....'

第一層是網路層,包含源、目的mac、ip協議號,第二層是tcp層,第三層包含埠號、http報文
其中每一層均為上一層的payload成員

閱讀全文

與scapyallpython相關的資料

熱點內容
飛行解壓素材 瀏覽:498
51單片機指令用背嗎 瀏覽:936
unityai演算法 瀏覽:834
我的世界ice伺服器如何打開pvp 瀏覽:975
c語言編程如何做標記 瀏覽:884
python數據分析實戰pdf 瀏覽:985
u盤插入文件夾 瀏覽:918
華為amd雲伺服器 瀏覽:497
漢化編程卡是什麼意思 瀏覽:128
python學習pdf 瀏覽:315
祝緒丹程序員那麼可愛拍吻戲 瀏覽:200
asp源碼會員消費系統 瀏覽:115
java反射設置 瀏覽:154
python一行文 瀏覽:441
排序演算法優缺點 瀏覽:565
惡搞加密文件pdf 瀏覽:674
gif怎麼壓縮圖片大小 瀏覽:219
命令選擇當前不可用 瀏覽:158
歐幾里得演算法如何求逆元 瀏覽:506
男中學生上課解壓神器 瀏覽:373