--- /dev/null
+#!/usr/bin/env python
+
+import sys, os, os.path, socket
+from optparse import OptionParser, Values
+
+VERSION = '''$Revision$'''
+
+try:
+ # These are my libraries...
+ import GregDebug, AsyncSocket
+
+ from GregDebug import debug, setDebugLevel, DEBUG_LEVEL_DEBUG, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_HIGH, DEBUG_INCREMENT
+except ImportError, e:
+ print >>sys.stderr, "Missing libraries!\nExiting..."
+ sys.exit(1)
+
+
+class NetClient(object):
+ def __init__(self):
+ self.async_handler = AsyncSocket.AsyncSocketOwner()
+
+ def buildparser(self):
+ parser = OptionParser(version="%prog " + VERSION,
+ description = "Connects to a specific server",
+ usage = "%prog [options] server [port]")
+ parser.add_option("-U", "--domain-socket",
+ action="store_true", dest="isUnixDomainSocket", default="",
+ help="Make the connection over a unix domain socket")
+ parser.add_option("-q", "--quiet", "--silent",
+ action="count", dest="quiet", default=0,
+ help="Make the script quiet (good for running from a shell script)")
+ parser.add_option("-v", '-d', "--verbose", "--debug",
+ action="count", dest="verbose", default=0,
+ help="Make the louder (good for debugging, or those who are curious)")
+ return parser
+
+ def createUnixSocket(self, domainSocketName):
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ sock.connect(domainSocketName)
+ return sock
+
+ def __handleStdin(self, fd):
+ data = fd.read()
+ if not data:
+ debug('Seems that stdin has been closed (got no data from in on the last read) - exiting main loop')
+ self.async_handler.exit() #XXX: I think this is the correct name
+ self.remoteSock.send(data) # Write the data to the socket
+ # TODO: Make sure that the data written to the socket has been completly written - Since
+ # self.sock is in non-blocking mode it may not have send everything
+
+ def __handleSock(self, fd):
+ data = fd.read()
+ if not data:
+ debug('Seems that the socket has been closed (got no data from in on the last read) - exiting main loop')
+ self.async_handler.doExit() #XXX: I think this is the correct name
+ sys.stdout.write(data) # Write the data to the screen
+ sys.stdout.flush()
+ # TODO: Turn sys.stdout into a non-blocking socket, since this call could block, and make things
+ # non-responsive
+
+ def main(self):
+ parser = self.buildparser()
+ useroptions, params = parser.parse_args(sys.argv[1:])
+
+ setDebugLevel(DEBUG_INCREMENT * (useroptions.quiet - useroptions.verbose))
+ debug("Just set GregDebug.DEBUG_LEVEL to %d" % GregDebug.DEBUG_LEVEL, DEBUG_LEVEL_LOW)
+
+ if useroptions.isUnixDomainSocket:
+ self.remoteSock = self.createUnixSocket(params[0])
+ else:
+ print >>sys.stderr, "No connection type specified"
+ sys.exit(1)
+
+ self.async_handler.addFD(sys.stdin, self.__handleStdin)
+ self.async_handler.addSocket(self.remoteSock, self.__handleSock)
+ self.async_handler.mainLoop()
+
+if __name__ == "__main__":
+ NetClient().main()