日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python执行效率有多低_python – Scapy的低性能

發布時間:2023/12/10 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python执行效率有多低_python – Scapy的低性能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在創建一個腳本,將來自Tap0的所有流量發送到Eth0,并將來自Eth0的所有流量發送到Tap0.在網上找到很多例子后,我設法讓它發揮作用.我遇到的問題是性能非常低.

在不使用腳本的情況下在2個VM之間進行ping操作,所需時間不到1毫秒.使用腳本需要大約15ms.

當我使用scp從VM向另一個VM發送10 MB文件時,平均值.沒有腳本,傳輸速率是12 Mbps.使用該腳本,它降至不到1 Mbps.

我知道Python實際上并不是處理網絡流量最快的語言,但它是否會變慢?

有沒有辦法優化這段代碼?

我的VM是Ubuntu 10.04 32位.

這是代碼:

import os,sys,getopt,struct,re,string,logging

from socket import *

from fcntl import ioctl

from select import select

from scapy.all import *

TUNSETIFF = 0x400454ca

IFF_TAP = 0x0002

TUNMODE = IFF_TAP

ETH_IFACE = "eth0"

TAP_IFACE = "tap0"

conf.iface = ETH_IFACE

# Here we capture frames on ETH0

s = conf.L2listen(iface = ETH_IFACE)

# Open /dev/net/tun in TAP (ether) mode (create TAP0)

f = os.open("/dev/net/tun", os.O_RDWR)

ifs = ioctl(f, TUNSETIFF, struct.pack("16sH", "tap%d", TUNMODE))

# Speed optimization so Scapy does not have to parse payloads

Ether.payload_guess=[]

os.system("ifconfig eth0 0.0.0.0")

os.system("ifconfig tap0 192.168.40.107")

os.system("ifconfig tap0 down")

os.system("ifconfig tap0 hw ether 00:0c:29:7a:52:c4")

os.system("ifconfig tap0 up")

eth_hwaddr = get_if_hwaddr('eth0')

while 1:

r = select([f,s],[],[])[0] #Monitor f(TAP0) and s(ETH0) at the same time to see if a frame came in.

#Frames from TAP0

if f in r: #If TAP0 received a frame

# tuntap frame max. size is 1522 (ethernet, see RFC3580) + 4

tap_frame = os.read(f,1526)

tap_rcvd_frame = Ether(tap_frame[4:])

sendp(tap_rcvd_frame,verbose=0) #Send frame to ETH0

#Frames from ETH0

if s in r: #If ETH0 received a frame

eth_frame = s.recv(1522)

if eth_frame.src != eth_hwaddr:

# Add Tun/Tap header to frame, convert to string and send. "\x00\x00\x00\x00" is a requirement when writing to tap interfaces. It is an identifier for the Kernel.

eth_sent_frame = "\x00\x00\x00\x00" + str(eth_frame)

os.write(f, eth_sent_frame) #Send frame to TAP0

總結

以上是生活随笔為你收集整理的python执行效率有多低_python – Scapy的低性能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。