]> code.delx.au - bg-scripts/blob - ncat.py
wallchanger: Fixed multiple monitors on OSX again.
[bg-scripts] / ncat.py
1 #!/usr/bin/env python
2
3 import sys, os, os.path, socket
4 from optparse import OptionParser, Values
5
6 VERSION = '''$Revision$'''
7
8 try:
9 # These are my libraries...
10 import GregDebug, AsyncSocket
11
12 from GregDebug import debug, setDebugLevel, DEBUG_LEVEL_DEBUG, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_HIGH, DEBUG_INCREMENT
13 except ImportError, e:
14 print >>sys.stderr, "Missing libraries!\nExiting..."
15 sys.exit(1)
16
17
18 class NetClient(object):
19 def __init__(self):
20 self.async_handler = AsyncSocket.AsyncSocketOwner()
21
22 def buildparser(self):
23 parser = OptionParser(version="%prog " + VERSION,
24 description = "Connects to a specific server",
25 usage = "%prog [options] server [port]")
26 parser.add_option("-U", "--domain-socket",
27 action="store_true", dest="isUnixDomainSocket", default="",
28 help="Make the connection over a unix domain socket")
29 parser.add_option("-q", "--quiet", "--silent",
30 action="count", dest="quiet", default=0,
31 help="Make the script quiet (good for running from a shell script)")
32 parser.add_option("-v", '-d', "--verbose", "--debug",
33 action="count", dest="verbose", default=0,
34 help="Make the louder (good for debugging, or those who are curious)")
35 return parser
36
37 def createUnixSocket(self, domainSocketName):
38 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
39 sock.connect(domainSocketName)
40 return sock
41
42 def __handleStdin(self, fd):
43 data = fd.read()
44 if not data:
45 debug('Seems that stdin has been closed (got no data from in on the last read) - exiting main loop')
46 self.async_handler.exit() #XXX: I think this is the correct name
47 self.remoteSock.send(data) # Write the data to the socket
48 # TODO: Make sure that the data written to the socket has been completly written - Since
49 # self.sock is in non-blocking mode it may not have send everything
50
51 def __handleSock(self, fd):
52 data = fd.read()
53 if not data:
54 debug('Seems that the socket has been closed (got no data from in on the last read) - exiting main loop')
55 self.async_handler.doExit() #XXX: I think this is the correct name
56 sys.stdout.write(data) # Write the data to the screen
57 sys.stdout.flush()
58 # TODO: Turn sys.stdout into a non-blocking socket, since this call could block, and make things
59 # non-responsive
60
61 def main(self):
62 parser = self.buildparser()
63 useroptions, params = parser.parse_args(sys.argv[1:])
64
65 setDebugLevel(DEBUG_INCREMENT * (useroptions.quiet - useroptions.verbose))
66 debug("Just set GregDebug.DEBUG_LEVEL to %d" % GregDebug.DEBUG_LEVEL, DEBUG_LEVEL_LOW)
67
68 if useroptions.isUnixDomainSocket:
69 self.remoteSock = self.createUnixSocket(params[0])
70 else:
71 print >>sys.stderr, "No connection type specified"
72 sys.exit(1)
73
74 self.async_handler.addFD(sys.stdin, self.__handleStdin)
75 self.async_handler.addSocket(self.remoteSock, self.__handleSock)
76 self.async_handler.mainLoop()
77
78 if __name__ == "__main__":
79 NetClient().main()