3
3
import vdebug .log
4
4
import base64
5
5
import time
6
+ import os
6
7
7
8
""" Response objects for the DBGP module."""
8
9
@@ -381,20 +382,31 @@ class Connection:
381
382
address = None
382
383
isconned = 0
383
384
384
- def __init__ (self , host = '' , port = 9000 , timeout = 30 , input_stream = None ):
385
+ def __init__ (self , endpoint , timeout = 30 , input_stream = None ):
385
386
"""Create a new Connection.
386
387
387
388
The connection is not established until open() is called.
388
389
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
+ }
391
400
timeout -- time in seconds to wait for a debugger connection before giving up (default 30)
392
401
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.
393
405
"""
394
- self .port = port
395
- self .host = host
406
+ self .endpoint = endpoint
396
407
self .timeout = timeout
397
408
self .input_stream = input_stream
409
+ self .address_type = self .endpoint ['socket_type' ]
398
410
399
411
def __del__ (self ):
400
412
"""Make sure the connection is closed."""
@@ -404,15 +416,41 @@ def isconnected(self):
404
416
"""Whether the connection has been established."""
405
417
return self .isconned
406
418
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
+
407
433
def open (self ):
408
434
"""Listen for a connection from the debugger. Listening for the actual
409
435
connection is handled by self.listen()."""
410
436
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 )
412
441
try :
413
- serv .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
414
442
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
+
416
454
serv .listen (5 )
417
455
(self .sock , self .address ) = self .listen (serv , self .timeout )
418
456
self .sock .settimeout (None )
0 commit comments