I am trying to write a Python program that listens in the background (as a daemon) on a defined port and IP and then validate incoming data (fixed CSV format output from a PBX), in that it verifies the source IP, and performs a check against the date field (if year == 1899 then set a missed call indicator in the insert) If valid, inserts record into a PostgreSQL database, otherwise log to syslog with an error. I figured I should use socket to listen to the specific port and IP?
import socket
IP = 192.168.1.1 # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Am I doing everything right? How do I validate IP and perform the required tasks? Thanks