get the source IP address of a tcp connection request [duplicate] - python

This question already has answers here:
Refusing connection from a host
(4 answers)
How to find the source ip and port of a client that wants to connect to a listening socket?
(2 answers)
Getting the source IP address of incoming connection
(1 answer)
Deny a client's TCP connect request before accept()
(1 answer)
Closed 7 months ago.
so assume I have a socket called S, it's listening for tcp requests
IP address X sent a connection request, at this stage I have the option to accept it, but first I want to just print the IP address
there is no error I straight up don't have the slightest idea on how to do this, I tried googling, went thru the docs, nothing was of any help, thanks in advance guys

Related

Python TCP hole punching [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
I am currently working on a little p2p basic system in python and I would like to know how do you make a TCP hole punch. I already successfully did one in UDP but I want the more organized TCP connection for the rest.
And yes, i've already googled the question but it does not seem that everybody has the same answer nor does it work.
You have to use a little trick. If you know the IP address and port of the peer you want to connect to, say 192.168.1.2:8081 , then you can punch the hole by connecting directly to it (using the same IP, of course) and telling it what your real IP is:
import socket
peer_ip = "192.168.1.2"
peer_port = 8081
my_ip = "192.168.1.1"
my_port = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((peer_ip, peer_port))
sock.send("OPEN %s:%d\n" % (my_ip, my_port))
If you connect to it then the firewall should open a port from your IP address to the peer's IP address and port that you specified in the OPEN command (this is a TCP hole punch protocol called ASYNC3 , which is described on the Async3 website).
To test this, you can use nc to connect to the peer and say hi. This should work if you are behind a NAT and have a firewall (even if it's not the same firewall that opened the hole):
$ nc 192.168.1.2 8081
hello, world!

Why are there a time delay before I can reuse a opened port? [duplicate]

This question already has answers here:
Python: Binding Socket: "Address already in use"
(13 answers)
Closed 3 years ago.
When I do some programming with sockets in python
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
s.settimeout(0.1)
Without closing the socket before exiting the problem, the port will be unavailable for a short amount of time.
I understand that I should properly close the socket before the program exits or closed. However, during development, there are cases where the program will crash/exit before I can take care of the socket.
Why is there a time delay before I can reuse the port again? and how can I avoid such problems?
It's because of the way TCP works. From memory the connection state is TIME WAIT state.
It's purpose is to prevent delayed packets arriving on a later stream.
Set SO_REUSEADDR option when opening the socket fixes this. See answer here:
Python: Binding Socket: "Address already in use"

Python ftplib with misconfigured passive connection [duplicate]

This question already has an answer here:
Cannot list FTP directory using ftplib – but FTP client works
(1 answer)
Closed 3 years ago.
I'm connecting to an external FTP/FTPS server using python3 ftplib.FTP_TLS class. When the connection is established in passive mode, the server responds with an internal network IP address like 10.10.XX.XX.
Since I am outside of the network I can't access the server on the provided IP address and ftplib hangs up. Setting the FTPS connection as active doesn't work.
What is the best way to force ftplib to use the original hostname or the external IP address?
The solution I used was from this article. You override the makepasv() method to ignore the returned IP address and use the original host:
class FTP_TLS_IgnoreHost(ftplib.FTP_TLS):
def makepasv(self):
_, port = super().makepasv()
return self.host, port
ftp = FTP_TLS_IgnoreHost('host', 'user', 'password')
There are probably other good solutions, but I thought this was pretty slick.

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

Detect Arduino port in Python [duplicate]

This question already has answers here:
Python to automatically select serial ports (for Arduino)
(4 answers)
Closed 6 years ago.
I am using an Arduino for sensing using Python 2.7 on Windows XP, but the non-static nature of the USB-to-serial port translation is giving me a headache. With a physical serial port there is no issue hard coding the port position, but the Arduino is moving around based on what is or is not plugged in at the time of object instantiation. Is there some way in Python for me to just get the port address during each object initialization and pass it to PyVISA or pySerial?
I also suggest a handshake but do it the other ay round.
Just READ for input from the all Serial ports before starting your program. As you turn up the Device you can make it send something like an ON signal. when your code detects the ON signal on that port then do a handshake.
In pySerial there is a quite hidden way to check for VID/PID on all serial ports (at least on Windows).
Just find the VID/PID of the Arduino in port properties adn put it into the python code.
Of course this won't work if you have multiple Arduino connected (same VID/PID)
import serial.tools.list_ports
for port in list(serial.tools.list_ports.comports()):
if port[2].startswith('USB VID:PID=1234:5678'):
#here you have the right port
I recommend a handshaking signal and scanning all the ports. For example, send "whoru" from your python script to the arduiono and have code on the arduiono that responds with "arduino" when it detects "whoru" on the serial port. This way, you scan the ports, send the handshake, and when you get the proper response you know which port the arduino is on.

Categories