Networking

UDP Client

From PyMOTW :

import socket
import sys

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = ('192.168.0.100', 8080)
message = 'This is the message.  It will be repeated.'

try:

    # Send data
    print >>sys.stderr, 'sending "%s"' % message
    sent = sock.sendto(message, server_address)

    # Receive response
    print >>sys.stderr, 'waiting to receive'
    data, server = sock.recvfrom(4096)
    print >>sys.stderr, 'received "%s"' % data

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

UDP Server

From PyMOTW :

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
while True:
    print >>sys.stderr, '\nwaiting to receive message'
    data, address = sock.recvfrom(4096)

    print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
    print >>sys.stderr, data

    if data:
        sent = sock.sendto(data, address)
        print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address)

Curl tips

curl -I -X DELETE  http://localhost/blah
curl --header "X-MyHeader: 123" www.google.com
curl -X POST --header "Content-Type: application/json" -d '{"test": true}' http://localhost/blah

If more than 10 telnet sessions to a server fail

per_source = 10 in /etc/xinetd.d/telnet or /etc/xinetd.conf

Start xinetd with debugs turned on

/usr/sbin/xinetd -f /etc/xinetd.conf -d

Check duplicate ip with arping

bash-4.2 ~$ arping 192.168.0.58 -D -c 3 -I ens32
ARPING 192.168.0.58 from 0.0.0.0 ens32
Unicast reply from 192.168.0.58 [18:E7:28:2E:92:9C]  1.747ms
Sent 1 probes (1 broadcast(s))
Received 1 response(s)
bash-4.2 ~$ echo $?
1
bash-4.2 ~$

Test tcp connections with nc

(from http://unix.stackexchange.com/questions/73767/how-to-check-whether-firewall-opened-for-a-port-but-not-listening-on-the-port)

nc -vz targetServer portNum

For example:

bash-3.2 ~$ nc -vz erdos 22
Connection to erdos 22 port [tcp/ssh] succeeded!
bash-3.2 ~$

Force NTP sync

sudo systemctl stop ntpd
sudo ntpdate -s time.nist.gov
sudo systemctl start ntpd