[Tutor] Raw Sockets
Rodrigues
op73418@mail.telepac.pt
Sun Aug 3 08:05:02 EDT 2003
> -----Original Message-----
> From: tutor-admin@python.org
> [mailto:tutor-admin@python.org]On Behalf Of
> G Kiran
> Sent: sábado, 2 de Agosto de 2003 16:04
> To: python tutor
> Subject: [Tutor] Raw Sockets
>
>
> Hi,
>
> Raw sockets are much misunderstod i guess.. python
> documentation does
> not provide much on this...though it just about says that
> its implemented.
> but similar calls to ones in unix donot work
>
> Specifically what i need to know is that how do i recv data
> from a raw
> socket...
> My reqmt is to monitor all traffic coming from an physical
> interface like a
> lan card in python.. without using any third party
> modules...with just
> socket libraries can i recv all the data coming in?
>
> even a lead...clue in this direction will be highly appreciated
>
The module socket and its documentation are your friend. Here I am
just going to fire up the interpreter and ask help for the socket
objects in the socket module (spelled socket.socket)
>>> help(socket.socket)
Help on class _socketobject in module socket:
class _socketobject(__builtin__.object)
| socket([family[, type[, proto]]]) -> socket object
|
| Open a socket of the given type. The family argument specifies
the
| address family; it defaults to AF_INET. The type argument
specifies
| whether this is a stream (SOCK_STREAM, this is the default)
| or datagram (SOCK_DGRAM) socket. The protocol argument defaults
to 0,
| specifying the default protocol. Keyword arguments are accepted.
|
| A socket object represents one endpoint of a network connection.
|
| Methods of socket objects (keyword arguments not allowed):
|
| accept() -- accept a connection, returning new socket and client
address
| bind(addr) -- bind the socket to a local address
| close() -- close the socket
| connect(addr) -- connect the socket to a remote address
| connect_ex(addr) -- connect, return an error code instead of an
exception
| dup() -- return a new socket object identical to the current one
[*]
| fileno() -- return underlying file descriptor
| getpeername() -- return remote address [*]
| getsockname() -- return local address
| getsockopt(level, optname[, buflen]) -- get socket options
| gettimeout() -- return timeout or None
| listen(n) -- start listening for incoming connections
| makefile([mode, [bufsize]]) -- return a file object for the socket
[*]
| recv(buflen[, flags]) -- receive data
| recvfrom(buflen[, flags]) -- receive data and sender's address
| sendall(data[, flags]) -- send all data
| send(data[, flags]) -- send data, may not send all of it
| sendto(data[, flags], addr) -- send data to a given address
| setblocking(0 | 1) -- set or clear the blocking I/O flag
| setsockopt(level, optname, value) -- set socket options
| settimeout(None | float) -- set or clear the timeout
| shutdown(how) -- shut down traffic in one or both directions
[rest snipped]
As you can see, there is a recv method for the socket object to fetch
data.
In the documentation, there is a How-to guide to sockets. You may want
to read it to get a feel for how things are done. Here I am just going
to post one of its examples:
#create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the web server on port 80
# - the normal http port
s.connect(("www.mcmillan-inc.com", 80))
Now you can use the recv method on s to fetch data.
Hope that helped, more question just hurl them.
G. Rodrigues
More information about the Tutor
mailing list