wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

dial.py (837B)


      1 from socket import *
      2 
      3 __all__ = 'dial',
      4 
      5 def dial_unix(address):
      6     sock = socket(AF_UNIX, SOCK_STREAM, 0)
      7     sock.connect(address)
      8     return sock
      9 
     10 def dial_tcp(host):
     11     host = host.split('!')
     12     if len(host) != 2:
     13         return
     14     host, port = host
     15 
     16     res = getaddrinfo(host, port, AF_INET, SOCK_STREAM, 0, AI_PASSIVE)
     17     for family, socktype, protocol, name, addr in res:
     18         try:
     19             sock = socket(family, socktype, protocol)
     20             sock.connect(addr)
     21             return sock
     22         except error:
     23             if sock:
     24                 sock.close()
     25 
     26 def dial(address):
     27     proto, address = address.split('!', 1)
     28     if proto == 'unix':
     29         return dial_unix(address)
     30     elif proto == 'tcp':
     31         return dial_tcp(address)
     32     else:
     33         raise Exception('invalid protocol')
     34 
     35 # vim:se sts=4 sw=4 et: