I want to implement tcp check with Python.
I found a magic way in this article.
It introduce a efficient way to do a tcp-health-check.
Client: SYN
Server: SYN-ACK
Client: RST
I also found a Go implementer https://github.com/tevino/tcp-shaker.
But i hope to implement SYN,SYN-ACK,RST in python.
My code is there:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_TCP, socket.TCP_QUICKACK, 0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
s.connect((ip, port))
s.close()
But it didn't work as I expected. The client send a ACK to the server when the SYN-ACK received from server.
How could I disable TCP_QUICKACK in Python?
Related
This is the configuration I am using.
Now I want the nodes to send packets in broadcast, but only to nodes directly connected.
sender.py:
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
server.settimeout(0.2)
message = b"mymessage"
while True:
server.sendto(message, ('<broadcast>', 37020))
print("message sent!")
time.sleep(1)
receiver.py:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 37020))
while True:
data, addr = client.recvfrom(1024)
print(f"received: {data}, from: {addr}\n")
Let's say this is the configuration:
If I run receiver.py on r1,r2,r3, and I run sender.py on r4, i would expect to receive the messages only on the directly connected machines, since I set TTL=1. Instead I receive the message in ALL machines. The output of the receiving machines is received b"mymessage" from 172.17.0.4 (which is the interface I use to manage the virtual nodes, as seen in the picture. Since I don't want the nodes to use that interface, I tried to replace the <broadcast> parameter in sender.py with a more specific one. I first replaced it with 10.255.255.255, but I couldn't receive anything. 0.0.0.0 is not working either. I have a similar issue using multicast, but that would go out of topic. I am probably doing something very wrong here but i can't figure out what.
The final aim, as partially stated above, is to give each nodes the ability to connect ONLY to the adjacent ones, skipping anything that requires more than 1 hop.
Thank you for your time.
I'm trying to implement a simple example of a distributed producer consumer, and while testing, I always get the following error:
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
Even though many posts claim that s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) before bind would allow me to ignore the TIME_WAIT, I still get the same exception when trying to connect for a second time.
server.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 4330))
s.listen()
s.accept()
s.close()
client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 4334))
s.connect((myaddress, 4330))
s.close()
When client socket closed locally, if you try to connect to the same destination again, with recreated socket of same ip:port, TIME_WAIT will be in effect, regardless of SO_REUSEADDR.
To avoid this, you can make the server to initiate close() first.
Please refer this answer and this article for more details.
I am writing a simple python proxy with Python 3.8 on Windows 10
If I use socket.accept I cannot terminate the process from console neither of these work: ctrl+c, ctrl+z, ctrl+d, break, ctrl+break, only closing the terminal.
I found in the docs this PIP https://www.python.org/dev/peps/pep-0475/ that is about retrying system calls on interrupts. I believe this is the reason why I cannot terminate the app.
Can anyone tell me a best practice how to terminate an app with a blocking socket.accept
Thanks in advance
my code:
import socket
bind_ip = "127.0.0.1"
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((bind_ip, bind_port))
server.listen(5)
print("[*] Listening on {}:{}".format(bind_ip, bind_port))
def handle_client(client_socket):
request = client_socket.recv(1024)
print("received: " + request.decode('ascii'))
client_socket.send("ACK".encode('ascii'))
client_socket.close()
while True:
client, addr = server.accept()
print("[*] accepted {}:{}".format(addr[0], addr[1]))
handle_client(client)
The socket might be in a TIME_WAIT state from the earlier trials & executions of your code.
You can use this flag to mitigate this, socket.SO_REUSEADDR
Under the Python Socket Documentation, you can set the flag like this.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
You can also look into setting a timeout for the socket!
I have got a server.py and a client.py.
If i run them on the same computer using the same port and host as 127.0.0.1 it works fine.
I got another laptop connected to the same network. I got my local ip address 129.94.209.9 and used that for the server. On the other laptop I tried connecting to the server but I couldn't.
Is this an issue with my code, with the network or I'm just using the wrong ip address?
Server.py
HOST = '129.94.209.9'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
sockfd, addr = server_socket.accept()
send and receive messages etc....
Client.py
HOST = '129.94.209.9'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
except:
print("Cannot connect to the server")
send and receive messages etc...
Client prints "Cannot connect to the server"
Thanks!!
UPDATES: thanks for the comments
1) I did do sockfd, addr = server_socket.accept() sorry I forgot to add it, it was a few lines further down in the code.
2) The error is: [Errno 11] Resource temporarily unavailable
3) Ping does work
EDIT: It is working now when I plug both computers in by ethernet cable to the same network. Not sure why my the won't work when they are right next to each other connected to wifi.
Thanks for all the suggestions! I'll investigate the network issue myself
Using the following code (my IP address rather than yours, of course) I see the expected behaviour.
so8srv.py:
import socket
HOST = '192.168.33.64'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
print("Listening")
s = server_socket.accept()
print("Connected")
so8cli.py:
import socket
HOST = '192.168.33.64'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
except Exception as e:
print("Cannot connect to the server:", e)
print("Connected")
When I run it, the server prints "Listening". When I then run the client, both it and the server print "Connected".
You will notice that, unlike your code, my client doesn't just print hte same diagnostic for any exception that's raised, but instead reports the exception. As a matter of sound practice you should avoid bare except clauses, since your program will then take the same action whether the exception is caused by a programming error or a user action such as KeyboardInterrupt.
Also try using this construction to obtain the IP address:
HOST=socket.gethostbyname(socket.gethostname())
Hello I am building an IM P2P client/server in Python and would like to use Tor as a proxy for it the server runs using Threads while the client runs at the same time
Does the server running on the local system need to listen to Tor? If so how do I do this? How do I make the client connect to a remote system using Tor?
I have searched for some examples but they lead to a library that seems to be really awkward to download
Here is the relevant server/client code
#Client Connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((str(self.CLIENT_HOST).strip(), self.CLIENT_PORT))
#Server
source_ip = ''
#socket.gethostbyname(socket.gethostname())
PORT = 9001
### Initialize socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((source_ip, PORT))
#
server_socket.listen(5)
Any ideas are greatly welcomed and appreciated