updated hphack.py

Jeff jam at dts.emich.edu
Thu Oct 12 15:39:46 EDT 2000


greetings,

I wrote hphack.py based on some code I found that was posted to
freshmeat.net and written in C. 

I wrote the python version mostly to show the differences between a C
implementation of a simple socket using program and it's python equivalent--
the python code is *much* shorter and IMHO much more readable.

the main change from the original python version was to make the bulk of the
code into a seperate function so it can be called from other modules.

another change is that it is no longer necessary to escape the text message
from the shell with quotes; when running the script from a shell, all
arguments to the script other than the first one are joined together and
passed to the hphack function.

I hope someone finds this code useful.. I've used it here at EMU to confuse
people walking by the printer (i.e. "practical joke" type messages), so at
the very least it's good for a laugh. ;)

I'm always open to suggestions for improving the code-- post 'em if ya got
'em.

regards,
J


-- begin hphack.py --
#!/usr/bin/env python

import sys
import string
import getopt
import socket

_RCSID = "$Id: hphack.py,v 1.4 2000/10/12 19:30:21 jam Exp $"

# based upon an idea and code from sili at l0ft.com, with assistance from
# comp.lang.python

# http://cssweb.nectech.com/printers/pclcodes/pcl5hp.htm
# http://freshmeat.net/appindex/2000/06/22/961716715.html

UEL = "\033%-12345X"

def version():
        print _RCSID

def hphack(dest, message):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(dest, 9100)
        msg = '@PJL RDYMSG DISPLAY="%s"\n' % (message)
        buf = string.join( (UEL,msg,UEL),"")+"\n"
        s.send(buf)
        s.close()
        return 0

if __name__ == "__main__":
        opts, sopts = getopt.getopt(sys.argv[1:], "", [ "help", "version" ])
        for n, v in opts:
                if n == "--help":
                        print "usage: hphack <ipaddress> \"<message>\""
                        sys.exit(0)
                elif n == "--version":
                        version()
                        sys.exit(0)
        hphack(sys.argv[1], string.join(sys.argv[2:]))
-- end hphack.py --

-- 
|| resnet 2000 -- <http://www.resnet.emich.edu/>
|| psa member -- <http://www.python.org/psa/> 




More information about the Python-list mailing list