33import vdebug .log
44import base64
55import time
6+ import os
67
78""" Response objects for the DBGP module."""
89
@@ -381,20 +382,31 @@ class Connection:
381382 address = None
382383 isconned = 0
383384
384- def __init__ (self , host = '' , port = 9000 , timeout = 30 , input_stream = None ):
385+ def __init__ (self , endpoint , timeout = 30 , input_stream = None ):
385386 """Create a new Connection.
386387
387388 The connection is not established until open() is called.
388389
389- host -- host name where debugger is running (default '')
390- port -- port number which debugger is listening on (default 9000)
390+ endpoint -- dictionary containing connection parameters
391+ {
392+ "socket_type": "tcp" (default)/"unix",
393+ # for TCP sockets
394+ "server": "localhost",
395+ "port": 9000,
396+ # for UNIX-domain sockets
397+ "unix_path": "/path/to/socket",
398+ "unix_permissions": 0777,
399+ }
391400 timeout -- time in seconds to wait for a debugger connection before giving up (default 30)
392401 input_stream -- object for checking input stream and user interrupts (default None)
402+
403+ Both "server" and "port" parameters are required for TCP sockets, for UNIX-domain sockets
404+ only "unix_path" is required.
393405 """
394- self .port = port
395- self .host = host
406+ self .endpoint = endpoint
396407 self .timeout = timeout
397408 self .input_stream = input_stream
409+ self .address_type = self .endpoint ['socket_type' ]
398410
399411 def __del__ (self ):
400412 """Make sure the connection is closed."""
@@ -404,15 +416,41 @@ def isconnected(self):
404416 """Whether the connection has been established."""
405417 return self .isconned
406418
419+ def address_description (self ):
420+ """Human-readable local address"""
421+ if self .endpoint ['socket_type' ] == 'tcp' :
422+ return "%s:%s" % (self .endpoint ['server' ],self .endpoint ['port' ])
423+ else :
424+ return self .endpoint ['unix_path' ]
425+
426+ def peer_description (self ):
427+ """Human-readable peer address"""
428+ if self .endpoint ['socket_type' ] == 'tcp' :
429+ return "%s:%s" % (self .address [0 ],self .address [1 ])
430+ else :
431+ return self .endpoint ['unix_path' ]
432+
407433 def open (self ):
408434 """Listen for a connection from the debugger. Listening for the actual
409435 connection is handled by self.listen()."""
410436 print 'Waiting for a connection (Ctrl-C to cancel, this message will self-destruct in ' ,self .timeout ,' seconds...)'
411- serv = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
437+ if self .address_type == 'tcp' :
438+ serv = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
439+ else :
440+ serv = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
412441 try :
413- serv .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
414442 serv .setblocking (0 )
415- serv .bind ((self .host , self .port ))
443+ if self .endpoint .get ('socket_type' , 'tcp' ) == 'tcp' :
444+ serv .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
445+ serv .bind ((self .endpoint ['server' ], self .endpoint ['port' ]))
446+ else :
447+ path = self .endpoint ['unix_path' ]
448+ if os .path .exists (path ):
449+ os .unlink (path )
450+ serv .bind (path )
451+ if self .endpoint .get ('unix_permissions' ):
452+ os .chmod (path , self .endpoint ['unix_permissions' ]);
453+
416454 serv .listen (5 )
417455 (self .sock , self .address ) = self .listen (serv , self .timeout )
418456 self .sock .settimeout (None )
0 commit comments