]>
git.ipfire.org Git - thirdparty/hostap.git/blob - wpaspy/wpaspy.py
3 # wpa_supplicant/hostapd control interface using Python
4 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
17 def __init__(self
, path
, port
=9877):
25 mode
= os
.stat(path
).st_mode
26 if stat
.S_ISSOCK(mode
):
34 self
.s
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_DGRAM
)
36 self
.local
= "/tmp/wpa_ctrl_" + str(os
.getpid()) + '-' + str(counter
)
38 self
.s
.bind(self
.local
)
40 self
.s
.connect(self
.dest
)
41 except Exception as e
:
48 ai_list
= socket
.getaddrinfo(path
, port
, socket
.AF_INET
,
50 for af
, socktype
, proto
, cn
, sockaddr
in ai_list
:
51 self
.sockaddr
= sockaddr
53 self
.s
= socket
.socket(af
, socktype
)
55 self
.s
.sendto(b
"GET_COOKIE", sockaddr
)
56 reply
, server
= self
.s
.recvfrom(4096)
60 print("connect exception ", path
, str(port
))
73 except Exception as e
:
74 # Need to ignore this allow the socket to be closed
83 def request(self
, cmd
, timeout
=10):
88 except UnicodeDecodeError as e
:
91 self
.s
.sendto(self
.cookie
+ cmd
, self
.sockaddr
)
94 [r
, w
, e
] = select
.select([self
.s
], [], [], timeout
)
96 res
= self
.s
.recv(4096).decode()
99 except UnicodeDecodeError as e
:
102 raise Exception("Timeout on waiting response")
107 res
= self
.request("ATTACH")
111 raise Exception("ATTACH failed")
114 if not self
.attached
:
116 while self
.pending():
118 res
= self
.request("DETACH")
119 if "FAIL" not in res
:
120 self
.attached
= False
122 raise Exception("DETACH failed")
128 except Exception as e
:
129 # Need to ignore this to allow the socket to be closed
130 self
.attached
= False
131 self
.request("TERMINATE")
134 def pending(self
, timeout
=0):
135 [r
, w
, e
] = select
.select([self
.s
], [], [], timeout
)
141 res
= self
.s
.recv(4096).decode()
144 except UnicodeDecodeError as e
: