]>
code.delx.au - bg-scripts/blob - ncat.py
3 import sys
, os
, os
.path
, socket
4 from optparse
import OptionParser
, Values
6 VERSION
= '''$Revision$'''
9 # These are my libraries...
10 import GregDebug
, AsyncSocket
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..."
18 class NetClient(object):
20 self
.async_handler
= AsyncSocket
.AsyncSocketOwner()
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)")
37 def createUnixSocket(self
, domainSocketName
):
38 sock
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
)
39 sock
.connect(domainSocketName
)
42 def __handleStdin(self
, fd
):
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
51 def __handleSock(self
, fd
):
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
58 # TODO: Turn sys.stdout into a non-blocking socket, since this call could block, and make things
62 parser
= self
.buildparser()
63 useroptions
, params
= parser
.parse_args(sys
.argv
[1:])
65 setDebugLevel(DEBUG_INCREMENT
* (useroptions
.quiet
- useroptions
.verbose
))
66 debug("Just set GregDebug.DEBUG_LEVEL to %d" % GregDebug
.DEBUG_LEVEL
, DEBUG_LEVEL_LOW
)
68 if useroptions
.isUnixDomainSocket
:
69 self
.remoteSock
= self
.createUnixSocket(params
[0])
71 print >>sys
.stderr
, "No connection type specified"
74 self
.async_handler
.addFD(sys
.stdin
, self
.__handleStdin
)
75 self
.async_handler
.addSocket(self
.remoteSock
, self
.__handleSock
)
76 self
.async_handler
.mainLoop()
78 if __name__
== "__main__":