1
1
import os
2
2
import socket
3
3
import websocket
4
+ import struct
4
5
5
6
import sys
6
7
if sys .version_info >= (3 , 0 ):
@@ -16,7 +17,6 @@ def resolve_hostname(hostname, port):
16
17
# to enforce we only search for IPV4 address
17
18
# and avoid a 5s timeout in the websocket on the ESP
18
19
# See https://github.com/esp8266/Arduino/issues/2110
19
-
20
20
addrinfo = socket .getaddrinfo (hostname , port ,
21
21
socket .AF_INET , 0 ,
22
22
socket .SOL_TCP )
@@ -32,7 +32,7 @@ def is_host_compatible(cls, host):
32
32
socket .inet_pton (socket .AF_INET , host )
33
33
return True
34
34
except socket .error :
35
- return host .endswith ('.local' )
35
+ return host .endswith ('.local' ) or ( host == "localhost" )
36
36
37
37
@classmethod
38
38
def available_hosts (cls ):
@@ -47,8 +47,8 @@ def available_hosts(cls):
47
47
def __init__ (self , host , port = 9342 ):
48
48
host = resolve_hostname (host , port )
49
49
50
- self ._ws = socket . socket ( socket . AF_INET , socket . SOCK_STREAM )
51
- self ._ws .connect ((host , port ))
50
+ self ._ws = websocket . WebSocket ( )
51
+ self ._ws .connect ("ws://" + str (host ) + ":" + str ( port )+ "/ws" )
52
52
53
53
self ._msg = queue .Queue (500 )
54
54
self ._running = True
@@ -68,7 +68,7 @@ def recv(self):
68
68
return data
69
69
70
70
def write (self , data ):
71
- self ._ws .send (data + ' \r ' . encode () + ' \n ' . encode () )
71
+ self ._ws .send (data )
72
72
73
73
def close (self ):
74
74
self ._running = False
@@ -92,8 +92,7 @@ def extract_line(s):
92
92
buff = b''
93
93
94
94
while self ._running :
95
- s = self ._ws .recv (4096 )
96
-
95
+ s = self ._ws .recv ()
97
96
buff = buff + s
98
97
while self ._running :
99
98
line , buff = extract_line (buff )
0 commit comments