473,657 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

low level networking in python

Hello,

I wish to do some low level network stuff using python.

I've googled somewhat and came up with pylibpcap[1], trouble is I
can't compile it on my Ubuntu 6.10 workstation. Can someone please
suggest a way to read some bits from random ports? I'm looking to
write a simple pen-testing tool that would try from one side
connecting to ports and from the other side sniff traffic to see on
what ports traffic is coming.

For pylibpcap I'm getting:

"""
hq4ever@lucky:~/development/personal/pylibpcap/pylibpcap-0.5.1$ python
setup.py build
running build
running build_ext
building '_pcapmodule' extension
swig -python -shadow -ISWIG -o pcap.c pcap.i
pcap.i:72: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
pcap.i:77: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
pcap.i:82: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
/usr/bin/python ./build-tools/docify.py pcap.c
/usr/bin/python ./build-tools/docify-shadow.py pcap.py
Traceback (most recent call last):
File "./build-tools/docify-shadow.py", line 30, in ?
raise 'source file doesn\'t look like swigged shadow class code'
source file doesn't look like swigged shadow class code
error: command '/usr/bin/python' failed with exit status 1
"""

[1] http://pylibpcap.sourceforge.net/
[2] http://py.vaults.ca/apyllo.py/126307487

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
Apr 3 '07 #1
6 3054
On Apr 3, 10:29 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
Hello,

I wish to do some low level network stuff using python.

I've googled somewhat and came up with pylibpcap[1], trouble is I
can't compile it on my Ubuntu 6.10 workstation. Can someone please
suggest a way to read some bits from random ports? I'm looking to
write a simple pen-testing tool that would try from one side
connecting to ports and from the other side sniff traffic to see on
what ports traffic is coming.

For pylibpcap I'm getting:

"""
hq4ever@lucky:~/development/personal/pylibpcap/pylibpcap-0.5.1$ python
setup.py build
running build
running build_ext
building '_pcapmodule' extension
swig -python -shadow -ISWIG -o pcap.c pcap.i
pcap.i:72: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
pcap.i:77: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
pcap.i:82: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
/usr/bin/python ./build-tools/docify.py pcap.c
/usr/bin/python ./build-tools/docify-shadow.py pcap.py
Traceback (most recent call last):
File "./build-tools/docify-shadow.py", line 30, in ?
raise 'source file doesn\'t look like swigged shadow class code'
source file doesn't look like swigged shadow class code
error: command '/usr/bin/python' failed with exit status 1
"""

[1]http://pylibpcap.sourc eforge.net/
[2]http://py.vaults.ca/apyllo.py/126307487

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
I would assume you could use the socket module. This post details
someone else who opened ports with Python:

http://www.thescripts.com/forum/thread44280.html

Here's another resource using some python servers:

http://docs.python.org/lib/socket-example.html

Finally, a Socket programming howto:
http://www.amk.ca/python/howto/sockets/

I'm also told that the Twisted framework is excellent for this sort of
thing.

Mike

Apr 3 '07 #2
ky******@gmail. com wrote:
On Apr 3, 10:29 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
>Hello,

I wish to do some low level network stuff using python.

I've googled somewhat and came up with pylibpcap[1], trouble is I
can't compile it on my Ubuntu 6.10 workstation. Can someone please
suggest a way to read some bits from random ports? I'm looking to
write a simple pen-testing tool that would try from one side
connecting to ports and from the other side sniff traffic to see on
what ports traffic is coming.

For pylibpcap I'm getting:

"""
hq4ever@lucky: ~/development/personal/pylibpcap/pylibpcap-0.5.1$ python
setup.py build
running build
running build_ext
building '_pcapmodule' extension
swig -python -shadow -ISWIG -o pcap.c pcap.i
pcap.i:72: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
pcap.i:77: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
pcap.i:82: Warning(124): Specifying the language name in %typemap is
deprecated - use #ifdef SWIG<LANGinstea d.
/usr/bin/python ./build-tools/docify.py pcap.c
/usr/bin/python ./build-tools/docify-shadow.py pcap.py
Traceback (most recent call last):
File "./build-tools/docify-shadow.py", line 30, in ?
raise 'source file doesn\'t look like swigged shadow class code'
source file doesn't look like swigged shadow class code
error: command '/usr/bin/python' failed with exit status 1
"""

[1]http://pylibpcap.sourc eforge.net/
[2]http://py.vaults.ca/apyllo.py/126307487

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?

I would assume you could use the socket module. This post details
someone else who opened ports with Python:

http://www.thescripts.com/forum/thread44280.html

Here's another resource using some python servers:

http://docs.python.org/lib/socket-example.html

Finally, a Socket programming howto:
http://www.amk.ca/python/howto/sockets/

I'm also told that the Twisted framework is excellent for this sort of
thing.
The problem with dealing with ports is that you'd have to open each port
individually. So the OP, I presume, is trying to capture packets
generically as they arrive at a particular point int he processing cycle
to avoid that necessity.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 3 '07 #3
On 3 Apr 2007 08:43:57 -0700, ky******@gmail. com <ky******@gmail .comwrote:
On Apr 3, 10:29 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
Hello,

I wish to do some low level network stuff using python.

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?

I would assume you could use the socket module. This post details
someone else who opened ports with Python:

http://www.thescripts.com/forum/thread44280.html

Here's another resource using some python servers:

http://docs.python.org/lib/socket-example.html

Finally, a Socket programming howto:
http://www.amk.ca/python/howto/sockets/

I'm also told that the Twisted framework is excellent for this sort of
thing.
Thanks for the heads-up. The sockets howto was great help.

I'm trying to bind a non-blocking socket, here is my code:
"""
#!/usr/bin/env python

import socket, select
from time import sleep

s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s_nb10000.setbl ocking(0)

s_nb10000.bind( ('192.168.2.106 ', 10002))
s_nb10000.liste n(5)

while 1:
conn, addr = s_nb10000.accep t()
ready_to_read, ready_to_write, in_error = select.select([conn], [], [], 0)
print (ready_to_read, ready_to_write, in_error)
sleep(100)

s_nb10000.close ()
"""

And this is the exception I'm getting:
"""
python non_blocking_so cket.py
Traceback (most recent call last):
File "non_blocking_s ocket.py", line 13, in ?
conn, addr = s_nb10000.accep t()
File "/usr/lib/python2.4/socket.py", line 161, in accept
sock, addr = self._sock.acce pt()
socket.error: (11, 'Resource temporarily unavailable')
"""

What am I doing wrong here?

p.s.
I've looked at twisted before posting this post. I've seen they
impelement alot of application level protocols but I didn't see much
treatment for low level "raw" network data, not to mention that it's a
way way over kill for what I'm asking to achieve. Twisted does have a
subproject called "Twisted Pair: Low-level networking" but sadly it's
unmaintained and undocumented.
Mike
Maxim.
--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
Apr 3 '07 #4
Maxim Veksler wrote:
I'm trying to bind a non-blocking socket, here is my code:
"""
#!/usr/bin/env python

import socket, select
from time import sleep

s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s_nb10000.setbl ocking(0)

s_nb10000.bind( ('192.168.2.106 ', 10002))
s_nb10000.liste n(5)

while 1:
conn, addr = s_nb10000.accep t()
ready_to_read, ready_to_write, in_error = select.select([conn], [],
[], 0)
print (ready_to_read, ready_to_write, in_error)
sleep(100)

s_nb10000.close ()
"""

And this is the exception I'm getting:
"""
python non_blocking_so cket.py
Traceback (most recent call last):
File "non_blocking_s ocket.py", line 13, in ?
conn, addr = s_nb10000.accep t()
File "/usr/lib/python2.4/socket.py", line 161, in accept
sock, addr = self._sock.acce pt()
socket.error: (11, 'Resource temporarily unavailable')
"""

What am I doing wrong here?
Nothing.
Any operation on a non-blocking socket that is usually blocking
(this includes accept(), bind(), connect(), recv with MSG_WAITALL)
can possibly return a socket.error with errno set to EAGAIN.
('resource temporarily unavailable').
If this happens you should use a select() on the socket to
wait until it's done with the requested operation.

--Irmen
>
p.s.
I've looked at twisted before posting this post. I've seen they
impelement alot of application level protocols but I didn't see much
treatment for low level "raw" network data, not to mention that it's a
way way over kill for what I'm asking to achieve. Twisted does have a
subproject called "Twisted Pair: Low-level networking" but sadly it's
unmaintained and undocumented.
>Mike

Maxim.

Apr 4 '07 #5
I wish to do some low level network stuff using python.
I've googled somewhat and came up with pylibpcap[1], trouble is I
can't compile it on my Ubuntu 6.10 workstation.
I would suggest pcapy:
http://oss.coresecurity.com/projects/pcapy.html
Higher level, easier Object-Oriented API and support for multi-
threading.
Here's an example showing a simple sniffer app:
http://oss.coresecurity.com/impacket/sniff.py
Anyway, what kind of 'low-level network stuff' we're talking about?

Apr 5 '07 #6
On 4/4/07, Irmen de Jong <ir**********@x s4all.nlwrote:
Maxim Veksler wrote:
I'm trying to bind a non-blocking socket, here is my code:
"""
#!/usr/bin/env python

import socket, select
from time import sleep

s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s_nb10000.setbl ocking(0)

s_nb10000.bind( ('192.168.2.106 ', 10002))
s_nb10000.liste n(5)

while 1:
conn, addr = s_nb10000.accep t()
ready_to_read, ready_to_write, in_error = select.select([conn], [],
[], 0)
print (ready_to_read, ready_to_write, in_error)
sleep(100)

s_nb10000.close ()
"""

And this is the exception I'm getting:
"""
python non_blocking_so cket.py
Traceback (most recent call last):
File "non_blocking_s ocket.py", line 13, in ?
conn, addr = s_nb10000.accep t()
File "/usr/lib/python2.4/socket.py", line 161, in accept
sock, addr = self._sock.acce pt()
socket.error: (11, 'Resource temporarily unavailable')
"""

What am I doing wrong here?

Nothing.
Any operation on a non-blocking socket that is usually blocking
(this includes accept(), bind(), connect(), recv with MSG_WAITALL)
can possibly return a socket.error with errno set to EAGAIN.
('resource temporarily unavailable').
If this happens you should use a select() on the socket to
wait until it's done with the requested operation.
Hello everyone, I would like to thank you all for the helping tips so
far, with your help I managed to improve the previous code to not give
the error, I believe it's now working.

The non blocking echo socket code:
"""
#!/usr/bin/env python

import socket, select

s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s_nb10000.setbl ocking(0)

s_nb10000.bind( ('0.0.0.0', 10002))
s_nb10000.liste n(5)

while 1:
ready_to_read, ready_to_write, in_error =
select.select([s_nb10000], [], [], 0)
if s_nb10000 in ready_to_read:
conn, addr = s_nb10000.accep t()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

s_nb10000.close ()
"""
--Irmen
Maxim.
--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
Apr 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3279
by: Fernando Rodriguez | last post by:
Hi, Any recommendation for a good book on multithreaded programming with Python? O:-) TIA
3
3811
by: Phil Frost | last post by:
Greetings all. I'm attempting to embed a python interpreter at a very low level in an OS I am writing. Currently I'm stuck with build issues. Firstly, as there is no working C compiler in our OS, I must cross compile Python. I encountered errors when building Parser/pgen; I got around this by building it nativly and copying it. Hopefully this won't get me later... My current problem (and there are sure to be more ;) ) is when building...
5
1908
by: ias0nas | last post by:
Is it possible to have low level netwoking with python under Windows? Like raw sockets? Is it possible to send a single packet using python under windows? Thank you
4
1993
by: Paulo Pinto | last post by:
Hello, Is there any Python library similar to NET::FTP from Perl? ftplib seems too lowlevel. I already found a few, but would like to get one that is endorsed by the community. Thanks, Paulo
7
8920
by: Brian Keogh | last post by:
Hi, I'm a student learning TCP/IP networking at University. Although I understand all about TCP/IP Networking in Java I am expected to understand the basics of C with regard to these programs as all the examples in my course notes and Exams refer to C. Can someone please point me in the right direction to where I might find answers on the web explaining C code involved with TCP/IP networking and what they mean. These are quite basic I'm...
46
2095
by: H.A. Sujith | last post by:
Why doesn't the standard library provide (at least basic) networking facilities using TCP/IP ?
2
2285
by: robert | last post by:
When employing complex UI libs (wx, win32ui, ..) and other extension libs, nice "only Python stack traces" remain a myth. Currently I'm hunting again a rare C-level crash bug of a Python based Windows app with rare user reports - and still in the dark (I get snippets of machine stack traces / screenshots with random "mem. access error" / "python caused the runtime to terminate in an unusual way" / ..) I'd like to hook a kind of quality...
65
5247
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective than the descriptions of OOP I've read in making a case. Ie., what objective data/studies/research indicates that a particular problem can be solved more quickly by the programmer, or that the solution is more efficient in execution time/memory...
0
875
by: Victor Bay | last post by:
Hi, pythoners, I am planning a face-to-face meetup on RAD with python: http://python.meetup.com/190/ Depends on the time commitment frame of python developers, we meet monthly, weekly, or even daily, at convenient place like coffee society, library, bookstore, ..., etc. I will also be trying to keep a concurrent blogs -- in text, pictures, videos -- to keep track of activities and topics as much as possible.
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.