Python Programming Resources
Python Programming Examples
Python UDP Beacon Server (run on server side)
#!/usr/bin/python
# FILE:
# -----
# beacon-srvr.py
#
# DESCRIPTION:
# ------------
# Purpose is to open a socket and to send a message to a server which reflects the
# message back to this client whereby it is displayed on the screen.
#
# SAMPLE COMMAND LINE:
# --------------------
# ./server-beacon-anyif.py 66.203.222.222 8881 [debug]
#
#
# Command Line Arguments:
# -----------------------
# ARGV[0] -> script name
# ARGV[1] -> Interface IP Address to Bind to
# ARGV[2] -> Port Number to Bind to
# ARGV[3] -> Debug indicator (use "debug" or "nodebug" on commandline)
#
# Modification History:
# ---------------------
# 09.30.2012 - Script created.
# 04.12.2012 - Script Updated to add command line arguments and variable
# binding parameter specification. As well as to be cleaned up for
# product deployment for a potential Pilot.
#
import socket
import sys
import string
import os
import time
import array
# --------------------- CONSTANTS ----------------------
# >>----------------- END CONSTANTS --------------------
# --------------------- FUNCTIONS ----------------------
# >>------------------ END FUNCTIONS ----------------------
# ------------------------- MAIN --------------------------
# os.system('clear')
# Command-Line Argument assignment and processing:
bindIP = sys.argv[1]
bindPortNumber = sys.argv[2]
debug = sys.argv[3]
# Socket Establishment
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#sock.bind(('66.203.222.222',8881))
sock.bind((bindIP, int(bindPortNumber)))
# loop waiting for datagrams
# (terminate with Ctrl-Break on Win32, Ctrl-C on Unix)
# Data received has the following format (stored in
# dlist):
# dlist[0]: cpeid
# dlist[1]: cpeid WAN_US Interface IP address
# dlist[2]: epoch time (seconds)
# dlist[3]: destination IP address of heartbeat
# dlist[4]: destination port address
#
try:
while True:
data, address = sock.recvfrom(8192)
dlist = data.split(',')
hbFileName = 'hb-' + dlist[0] + '.dat'
if debug == 'debug':
print 'RECEIVED INFORMATION...'
print '-----------------------'
print ' CPEID: ', dlist[0]
print ' Source IP: ', dlist[1]
print ' Outside IP: ', address[0]
print 'Source Port: ', address[1]
print ' TIME: ', dlist[2]
print ' '
_heartbeat = open(hbFileName,'w')
_heartbeat.write(dlist[0] + '\n')
_heartbeat.write(dlist[1] + '\n')
_heartbeat.write(str(address[0]) + '\n')
_heartbeat.write(str(address[1]) + '\n')
_heartbeat.write(dlist[2] + '\n')
_heartbeat.close()
data = str(address)
sock.sendto(data, address)
finally:
sock.close()
# ------------------------ END MAIN ------------------------
# FILE:
# -----
# beacon-srvr.py
#
# DESCRIPTION:
# ------------
# Purpose is to open a socket and to send a message to a server which reflects the
# message back to this client whereby it is displayed on the screen.
#
# SAMPLE COMMAND LINE:
# --------------------
# ./server-beacon-anyif.py 66.203.222.222 8881 [debug]
#
#
# Command Line Arguments:
# -----------------------
# ARGV[0] -> script name
# ARGV[1] -> Interface IP Address to Bind to
# ARGV[2] -> Port Number to Bind to
# ARGV[3] -> Debug indicator (use "debug" or "nodebug" on commandline)
#
# Modification History:
# ---------------------
# 09.30.2012 - Script created.
# 04.12.2012 - Script Updated to add command line arguments and variable
# binding parameter specification. As well as to be cleaned up for
# product deployment for a potential Pilot.
#
import socket
import sys
import string
import os
import time
import array
# --------------------- CONSTANTS ----------------------
# >>----------------- END CONSTANTS --------------------
# --------------------- FUNCTIONS ----------------------
# >>------------------ END FUNCTIONS ----------------------
# ------------------------- MAIN --------------------------
# os.system('clear')
# Command-Line Argument assignment and processing:
bindIP = sys.argv[1]
bindPortNumber = sys.argv[2]
debug = sys.argv[3]
# Socket Establishment
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#sock.bind(('66.203.222.222',8881))
sock.bind((bindIP, int(bindPortNumber)))
# loop waiting for datagrams
# (terminate with Ctrl-Break on Win32, Ctrl-C on Unix)
# Data received has the following format (stored in
# dlist):
# dlist[0]: cpeid
# dlist[1]: cpeid WAN_US Interface IP address
# dlist[2]: epoch time (seconds)
# dlist[3]: destination IP address of heartbeat
# dlist[4]: destination port address
#
try:
while True:
data, address = sock.recvfrom(8192)
dlist = data.split(',')
hbFileName = 'hb-' + dlist[0] + '.dat'
if debug == 'debug':
print 'RECEIVED INFORMATION...'
print '-----------------------'
print ' CPEID: ', dlist[0]
print ' Source IP: ', dlist[1]
print ' Outside IP: ', address[0]
print 'Source Port: ', address[1]
print ' TIME: ', dlist[2]
print ' '
_heartbeat = open(hbFileName,'w')
_heartbeat.write(dlist[0] + '\n')
_heartbeat.write(dlist[1] + '\n')
_heartbeat.write(str(address[0]) + '\n')
_heartbeat.write(str(address[1]) + '\n')
_heartbeat.write(dlist[2] + '\n')
_heartbeat.close()
data = str(address)
sock.sendto(data, address)
finally:
sock.close()
# ------------------------ END MAIN ------------------------
Python Beacon Client (run on client side)
#!/usr/bin/python
#
# FILE:
# -----
# client-beacon-anyif.py
#
# DESCRIPTION:
# ------------
# Purpose is to open a socket and to send a message to a server which reflects the
# message back to this client whereby it is displayed on the screen.
#
# SAMPLE COMMAND LINE:
# --------------------
# ./client-beacon-anyif.py 66.203.222.222 eth0 8881 cpeid1.dat 1 [debug | nodebug]
#
# Command Line Arguments:
# -----------------------
# ARGV[0] -> script name
# ARGV[1] -> Remote Secondary HCI IP Address
# ARGV[2] -> Local Interface serving as WAN_US (eg. "eth1")
# ARGV[3] -> Remote Bind Port Number at HCI.
# ARGV[4] -> Local CPEID.DAT file (name of file)
# ARGV[5] -> Transmission Rate (Every X Seconds)
# ARGV[6] -> Debug indicator (use "debug" or "nodebug" on commandline)
#
# Modification History:
# ---------------------
# 09.30.2012 - Script created.
#
import socket
import struct
import fcntl
import sys
import string
import os
import time
import array
# --------------------- CONSTANTS ----------------------
# >>----------------- END CONSTANTS --------------------
# --------------------- FUNCTIONS ----------------------
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#sockfd = sock.fileno()
#SIOCGIFADDR = 0x8915
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
# >>------------------ END FUNCTIONS ----------------------
# ------------------------- MAIN --------------------------
# os.system('clear')
# Command-Line Argument assignment and processing:
#
hciIP = sys.argv[1]
wanusIF = sys.argv[2]
bindPortNumber = sys.argv[3]
# Default cpeidFile
cpeidFile = 'cpeid.dat'
if len(sys.argv[4]) > 0:
cpeidFile = sys.argv[4]
transRate = int(sys.argv[5])
debug = sys.argv[6]
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# sock.setblocking(0)
localIF = get_ip_address(wanusIF)
#if debug = 'debug':
# print 'Local Interface Address is: ', localIF
# NOTE: Need to open CPEID file and copy out the CPEID for sending to
# the HEC.
_cpeidFile = open(cpeidFile, 'r')
cpeid = _cpeidFile.readline()
if debug == 'debug':
print cpeid + '<----'
_cpeidFile.close()
# "data" received from the HEC should be the outside address of the CPE's NAT
# firewall. This is seen from HEC's perspective. "address"
# Frequency of the beacon send (or heartbeat) is 5 seconds.
count = 0
while True:
# sock.sendto(cpeid + ',' + localIF + ',' + str(time.time()), (hciIP, int(bindPortNumber)))
sock.sendto(cpeid + ',' + localIF + ',' + str(time.time()), (hciIP, int(bindPortNumber)))
if debug == 'debug':
print 'SEND--->'
print cpeid + ',' + localIF + ',' + str(time.time())
os.system('date')
# data, address = sock.recvfrom(8192)
# print 'RECEIVE<---'
# os.system('date')
#dlist = data.split(',')
#hbFileName = 'hb-' + cpeid + '.dat'
#_heartbeat = open(hbFileName,'w')
#_heartbeat.write(str(data))
#_heartbeat.write(str(address) + '\n')
#_heartbeat.write(dlist[0] + '\n')
#_heartbeat.write(dlist[1] + '\n')
#_heartbeat.write(str(address) + '\n')
#_heartbeat.close()
#if debug == 'debug':
# print data, address
time.sleep(transRate)
# >>---------------------- END MAIN ------------------------
root@cpe20-a850:~#
#
# FILE:
# -----
# client-beacon-anyif.py
#
# DESCRIPTION:
# ------------
# Purpose is to open a socket and to send a message to a server which reflects the
# message back to this client whereby it is displayed on the screen.
#
# SAMPLE COMMAND LINE:
# --------------------
# ./client-beacon-anyif.py 66.203.222.222 eth0 8881 cpeid1.dat 1 [debug | nodebug]
#
# Command Line Arguments:
# -----------------------
# ARGV[0] -> script name
# ARGV[1] -> Remote Secondary HCI IP Address
# ARGV[2] -> Local Interface serving as WAN_US (eg. "eth1")
# ARGV[3] -> Remote Bind Port Number at HCI.
# ARGV[4] -> Local CPEID.DAT file (name of file)
# ARGV[5] -> Transmission Rate (Every X Seconds)
# ARGV[6] -> Debug indicator (use "debug" or "nodebug" on commandline)
#
# Modification History:
# ---------------------
# 09.30.2012 - Script created.
#
import socket
import struct
import fcntl
import sys
import string
import os
import time
import array
# --------------------- CONSTANTS ----------------------
# >>----------------- END CONSTANTS --------------------
# --------------------- FUNCTIONS ----------------------
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#sockfd = sock.fileno()
#SIOCGIFADDR = 0x8915
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
# >>------------------ END FUNCTIONS ----------------------
# ------------------------- MAIN --------------------------
# os.system('clear')
# Command-Line Argument assignment and processing:
#
hciIP = sys.argv[1]
wanusIF = sys.argv[2]
bindPortNumber = sys.argv[3]
# Default cpeidFile
cpeidFile = 'cpeid.dat'
if len(sys.argv[4]) > 0:
cpeidFile = sys.argv[4]
transRate = int(sys.argv[5])
debug = sys.argv[6]
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# sock.setblocking(0)
localIF = get_ip_address(wanusIF)
#if debug = 'debug':
# print 'Local Interface Address is: ', localIF
# NOTE: Need to open CPEID file and copy out the CPEID for sending to
# the HEC.
_cpeidFile = open(cpeidFile, 'r')
cpeid = _cpeidFile.readline()
if debug == 'debug':
print cpeid + '<----'
_cpeidFile.close()
# "data" received from the HEC should be the outside address of the CPE's NAT
# firewall. This is seen from HEC's perspective. "address"
# Frequency of the beacon send (or heartbeat) is 5 seconds.
count = 0
while True:
# sock.sendto(cpeid + ',' + localIF + ',' + str(time.time()), (hciIP, int(bindPortNumber)))
sock.sendto(cpeid + ',' + localIF + ',' + str(time.time()), (hciIP, int(bindPortNumber)))
if debug == 'debug':
print 'SEND--->'
print cpeid + ',' + localIF + ',' + str(time.time())
os.system('date')
# data, address = sock.recvfrom(8192)
# print 'RECEIVE<---'
# os.system('date')
#dlist = data.split(',')
#hbFileName = 'hb-' + cpeid + '.dat'
#_heartbeat = open(hbFileName,'w')
#_heartbeat.write(str(data))
#_heartbeat.write(str(address) + '\n')
#_heartbeat.write(dlist[0] + '\n')
#_heartbeat.write(dlist[1] + '\n')
#_heartbeat.write(str(address) + '\n')
#_heartbeat.close()
#if debug == 'debug':
# print data, address
time.sleep(transRate)
# >>---------------------- END MAIN ------------------------
root@cpe20-a850:~#
Downloadable Code Examples
clientbeacon.py | |
File Size: | 3 kb |
File Type: | py |
serverbeacon.py | |
File Size: | 2 kb |
File Type: | py |
createreplay3.py | |
File Size: | 7 kb |
File Type: | py |
ex.py | |
File Size: | 2 kb |
File Type: | py |
newnumlinesprog.py | |
File Size: | 1 kb |
File Type: | py |