Measuring socket to socket latency in Python - python

I'm using the Python socket library to send a message to another server and measure the roundtrip latency. I'm new networks so it's very basic, just using the send-receive examples given in the socket docs (http://wiki.python.org/moin/UdpCommunication). For the moment I'm using time.time() to timestamp when the message goes out and when it comes back, though I want to be able to get nanosecond accuracy in the end. Right now, I get about 300microseconds through my python script but pinging from the shell yields about 100micros. What can I do to get faster communication and/or more accurate measurement?
Script that sends one message:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
print "SENT {0}".format(int(time.time()*1000*1000)
This is the script that listens for the ping to come back
import socket
UDP_IP = "originating side localhost ip"
UDP_PORT = 9112
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
print "received message: {0} {1}".format(data, int(time.time()*1000*1000)
This is the script that listens on the server being pinged. When it receives a message on the port, it sends a message right back
import socket
UDP_IP = "pinged server's localhost ip"
UDP_PORT = 9111
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
UDP_IP2 = "ip of server that sent ping"
UDP_PORT2 = 9112
MESSAGE = "HEY"
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print "Listening on PORT 9111"
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
sock.sendto(MESSAGE, (UDP_IP2, UDP_PORT2))
print "received message: ", data

Related

Simple UDP application not receiving in Python

I am trying to create a small program that sends a Hello World to my laptop from my Raspberry Pi, I am on the same network however, the code below seems not to work. I feel like it might be a firewall issue.
Raspberry Pi's code
import socket
ip = "127.0.0.1"
port = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
message = b"Hello World"
sock.sendto(message, (ip, port))
Laptop's code
import socket
ip = "127.0.0.1"
port = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
while True:
data, addr = sock.recvfrom(1024)
print("received message: %s" % data)
There are no error messages, just no data is displayed.
Should be noted that when both of these files are ran on the same machine they work however, when on two different machines they do not.

"An integer is required error received for UDP server"

I try to communicate 2 raspberry pi's and I use UDP server. While creating udp server I used the codes below, however I took an error as it requires an integer at the line 10. Can you help me please ?
This is for Raspberry pi 3 and I try udp server.
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = "5005"
MESSAGE ="1"
print "UDP target IP:", UDP_IP
print " UDP target port:", UDP_PORT
print"message:", MESSAGE
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
I expect to create the sending server.
welcome to StackOverflow! I solved this by changing UDP_PORT to 5005 instead of "5005".
#/usr/bin/python
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE ="1"
print "UDP target IP:", UDP_IP
print " UDP target port:", UDP_PORT
print"message:", MESSAGE
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
It's asking for an integer and interpreting your argument "5005" as a string.

Python 2.7 -> How to send specific amount of TCP/UDP packets python

Im trying to create a script that will send a specific amount of packets with tcp/udp
i used the next script as a start to check how the socket module works and its working. but the massage is not at the size of 1 packet.
can anyone help?
Client:
import socket
UDP_IP = "0.0.0.0" # = 0.0.0.0 u IPv4
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
print "Socket is connected!", sock
for x in range(0,300):
print "send"
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

communication between 3 raspberry pi

I am trying to send data from 2 raspberry pi's ( model 2B) to a single raspberry pi through the ethernet port. I do the communication between 2 pi's via ethernet port. The code corresponding to that would be provided below.
Transmitter Side:
import socket
UDP_IP = "192.168.1.53"
UDP_Port = 1452
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_Port
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_Port))
**Receiver Side:**
import socket
UDP_IP = "0.0.0.0"
UDP_Port =1452
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet UDP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((UDP_IP, UDP_Port))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
How can receive data from 2 pis to a single raspberry pi through the ethernet port? Is it possible to do this?

UDP in Jython 2.7

I'm trying to write a python function that sends an UDP message to a remote host and receives a reply, but I have a really hard time understaning how to do this.
I've been looking at thread: Simple Python UDP Server: trouble receiving packets from clients other than localhost
I understand how to send something, but how to send AND receive in the correct sequence?
Thanks in advance.
Please ignore, I got it..
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 3001
MESSAGE = "asd"
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data

Categories