Skip to content

Instantly share code, notes, and snippets.

@iAnatoly
Last active May 13, 2022 15:34
Show Gist options
  • Save iAnatoly/f80088186410dd6a59c04a70aebb8ccf to your computer and use it in GitHub Desktop.
Save iAnatoly/f80088186410dd6a59c04a70aebb8ccf to your computer and use it in GitHub Desktop.
SIP ping
#!/usr/bin/env python3
import socket
import time
userid = "trivial_sip_pinger"
domain = "<sip server to test>"
port = 5060
timeout_ms = 1000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(timeout_ms / 1000.0)
sock.connect((domain,port))
ip,localport = sock.getsockname()
packet = """OPTIONS sip:{domain} SIP/2.0
Via: SIP/2.0/UDP {ip}:{localport}
To: "{userid}"<sip:{userid}@{domain}>
From: "{userid}"<sip:{userid}@{domain}>
Call-ID: nevermind
CSeq: 1 OPTIONS
Max-forwards: 10
Content-Length: 0
""".format(domain=domain, ip=ip, userid=userid, localport=localport)
# print(packet)
start = time.time()
sock.send(packet.encode('ascii'))
data, addr = sock.recvfrom(65535)
end = time.time()
diff = float("%.2f" % ((end - start) * 1000.0))
response = data.decode('ascii')
print("Timing: {} ms\n\nResponse: {}".format(diff,response))