Python-sniffing IP packets [duplicate] - python

This question already has answers here:
Packet sniffing in Python (Windows)
(6 answers)
Closed 7 years ago.
What is the appropriate way in python to sniff all IP packets in real time? I'm interested in getting the raw packet data.
I've read that raw sockets are supposed to let you read some packets but didn't quite get the hang of it.

I think it make sense to use pylibpcap. Which is wrapper on Linux libcap application for package capturing.

Related

python automatic email response system [duplicate]

This question already has answers here:
Receive and send emails in python
(10 answers)
Closed 7 years ago.
I am planning to create a program(with python) that analyzes and responds to emails on ubuntu, but I cant seem to find anything that could serve as the receiving and sending part of the program(in python, or with terminal commands) does anyone have any suggestions on what I could use?
You can use the libraries
poplib and imaplib for receiving emails,
smtplib for sending emails,
have a look at email to compose more complex emails.
You want to take a look at poplib and smtplib.

Perofrming a python-requests Request/Reponse transaction using files rather than a socket [duplicate]

This question already has answers here:
Python requests - print entire http request (raw)?
(9 answers)
Closed 8 years ago.
I am looking for a recipe for writing and reading the raw data generated by a requests transaction from files rather than a socket. By "raw data" I mean the bytes just before they are written to or read from the underlying socket. I've tried:
Using "hooks". This seems to be mostly deprecated as the only remaining hook is "response".
mount()ing a custom Adapter. Some aggressive duck-typing here provides access to the underlying httplib.HTTPConnection objects, but the call stack down there is complicated and quite brittle.
The final solution does not need to be general-purpose as I am only interested in vanilla HTTP functionality. I won't be streaming or using the edgier parts of the protocol.
Thanks!
Spawn a thread (import threading). Run an HTTP server in there. You can generate a unique port on demand by socket.socket().bind(0). In the HTTP server, just write the incoming data to a file (perhaps named by timestamp and incoming port number). Then send your requests there.

Python Getting LAN IP Address of Machine on Ubuntu [duplicate]

This question already has answers here:
Python - Get localhost IP [duplicate]
(2 answers)
Closed 9 years ago.
So I'm trying to get the LAN IP Address of the machine the program is running on and compare it to IP Addresses passed to it via UDP.
However when I use:
print str(socket.gethostbyname(socket.gethostname()))
It returns 127.0.0.1 which should be 192.168.1.9.
I've looked through the linux machine and its getting the IP Address of the lo (loopBack) port? I don't know exactly what that is but it should be getting the IP Address of eth0.
I've found that I can subprocess the bash command "ifconfig eth0" but that returns a big block of a string. I can process it down to what I need, but this is going to be running around 3 times a second on a beaglebone so I'd like it to be a little more effecient.
Is there a more elegant way of doing this?
Can I just change the target of gethostname?
Why is it targeting the lo port?
Thanks for your help maners.
Try returning the fully qualified domain name of the machine:
print str(socket.gethostbyname(socket.getfqdn()))
/etc/hosts probably has an entry resolving hostname to 127.0.0.1, which is why socket.gethostbyname() doesn't return what you expect.
Original question asked and answered here, but the socket.getfqdn() solution didn't stick out at a quick glance. Here's the solution for parsing ifconfig output if you decide to go that route. Standard library seems more than sufficient for solving your problem.
netifaces seems like a pretty sweet python module which should do the trick for you.

what does means this error "broken pipe"? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
TCP client-server SIGPIPE
I would like know what does this error mean?
You are using sockets and Http protocol.
It simply means your TCP connection has been closed by the other end or broken due to some other reason. By broken it means a 3 way handshake is required again before starting data transfer. As mentioned in the comments, being on listening end i.e. server, you normally cannot initiate the connection. So should simply close this socket and proceed ahead.
However, if you were a client, you should probably call api similar to connect again and proceed once it is successful.
Broken pipe on SO

What RPC module works over SSH, telnet and HTTP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What RPC module should I use to implement RCP in Python and be able to change connection method later?
I am looking for RPC solution that can be used over different protocols like SSH, telnet and HTTP.
It has to be Python 2.5 compatible.
You're likely going to have to roll your own, but much of the heavy lifting in transport code could be done in other modules:
paramiko for ssh
telnetlib for telnet
urllib(2) for http.
You'll still have to address the issue of data format, but that is independent of transport protocol (feel free to deliver XML-RPC or JSON or any other format over these transports).

Categories