@ -7,16 +7,30 @@
# See README for more details.
import os
import stat
import socket
import select
counter = 0
class Ctrl :
def __init__ ( self , path ):
def __init__ ( self , path , port = 9877 ):
global counter
self . started = False
self . attached = False
self . path = path
self . port = port
try :
mode = os . stat ( path ) . st_mode
if stat . S_ISSOCK ( mode ) :
self . udp = False
else :
self . udp = True
except :
self . udp = True
if not self . udp :
self . s = socket . socket ( socket . AF_UNIX , socket . SOCK_DGRAM )
self . dest = path
self . local = " /tmp/wpa_ctrl_ " + str ( os . getpid ( ) ) + ' - ' + str ( counter )
@ -28,6 +42,24 @@ class Ctrl:
self . s . close ( )
os . unlink ( self . local )
raise
else :
try :
ai_list = socket . getaddrinfo ( path , port , socket . AF_INET ,
socket . SOCK_DGRAM )
for af , socktype , proto , cn , sockaddr in ai_list :
self . sockaddr = sockaddr
break
self . s = socket . socket ( af , socktype )
self . s . settimeout ( 5 )
self . s . sendto ( " GET_COOKIE " , sockaddr )
reply , server = self . s . recvfrom ( 4096 )
self . cookie = reply
self . port = port
except :
print " connect exception " , path , str ( port )
if self . s != None :
self . s . close ( )
raise
self . started = True
def __del__ ( self ) :
@ -43,10 +75,14 @@ class Ctrl:
pass
if self . started :
self . s . close ( )
if not self . udp :
os . unlink ( self . local )
self . started = False
def request ( self , cmd , timeout = 10 ) :
if self . udp :
self . s . sendto ( self . cookie + cmd , self . sockaddr )
else :
self . s . send ( cmd )
[ r , w , e ] = select . select ( [ self . s ] , [ ] , [ ] , timeout )
if r :