#!/usr/pkg/bin/python """receive packet, for testing purposes just sit until packet arrives on specified port, then print it out""" Copyright = """ recv -- receive a packet and print it Copyright (C) 2004 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.sep.join([pwd.getpwuid(os.geteuid())[5], 'lib', 'python'])) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = sys.argv[0].split(os.sep)[-1] command = self.split('.')[0] # chop any suffix (extension) # now get name we gave it when we wrote it originalself = Copyright.split()[0] # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): return False # defined instead by pytest module, use that for debugging # other globals, specific to this program from socket import * def recv(*args): """listen for packet and print it... mainly for testing other software, but might have some practical uses """ try: while type(args[0]) != types.StringType: args = args[0] protocol, portstring = args except: sys.stderr.write("Usage: %s PROTOCOL PORT\n" % command) sys.exit(0) socket_type = SOCK_STREAM if protocol == 'udp': socket_type = SOCK_DGRAM port = int(portstring) receiver = socket(AF_INET, socket_type) receiver.bind(('0.0.0.0', port)) # note double parens, must be tuple print receiver.recv(1024) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... recv(sys.argv[1:]) else: # if you want something to be done on import, do it here; otherwise pass pass