-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathasync_operations.py
92 lines (78 loc) · 2.86 KB
/
async_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# This example shows how to use asyncio programming with PyShark.
# In the example below PyShark is executed in LiveCapture mode.
# During this operation another function is also called.
# Asyncio allows these two operations to run in concurrent with each other.
##################################################################################
# Python imports required for basic operations
##################################################################################
# Standard library imports
import asyncio
import threading
from typing import Any
import queue
# Third-party imports
import pyshark
# Import Packet class from pyshark.packet.packet
from pyshark.packet.packet import Packet
# Thread-safe queue to hold captured packets
packet_queue: queue.Queue = queue.Queue()
def capture_packets(interface: str) -> None:
"""
Captures packets on the specified network interface using pyshark.LiveCapture
and puts each packet into the packet_queue.
:param interface: The network interface to capture packets on.
:param type interface: str
"""
capture = pyshark.LiveCapture(interface=interface)
def packet_handler(packet: Any) -> None:
packet_queue.put(packet)
capture.apply_on_packets(packet_handler, timeout=100)
async def process_packets() -> None:
"""
Asynchronously processes packets from the packet_queue.
"""
while True:
try:
packet = packet_queue.get_nowait()
await process_packet(packet)
except queue.Empty:
await asyncio.sleep(0.1)
async def process_packet(packet: Packet) -> None:
"""
Processes an individual packet, extracting and printing IP and TCP information.
:param packet: The packet to process.
:param type packet: pyshark.packet.packet.Packet
"""
try:
if 'IP' in packet:
print(f'IP Packet: {packet.ip.src} -> {packet.ip.dst}')
if 'TCP' in packet:
print(f'TCP Packet: {packet.tcp.srcport} -> {packet.tcp.dstport}')
except AttributeError:
# Handle packets that don't have the expected attributes
pass
async def do_other_tasks() -> None:
"""
Simulates performing other asynchronous tasks.
"""
while True:
print("Performing other tasks...")
await asyncio.sleep(2) # Simulate doing other work
async def main() -> None:
"""
Main coroutine that starts the packet capture in a separate thread and
runs the packet processing and other tasks concurrently.
"""
interface = 'en0'
# Start the packet capture in a separate thread
capture_thread = threading.Thread(target=capture_packets, args=(interface,))
capture_thread.start()
await asyncio.gather(
process_packets(),
do_other_tasks()
)
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Shutting down...")