命令:
sudo mkdir /usr/local/python36
cd /home/jack/work
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
tar xvf Python-3.6.6.tar.xz
cd /home/jack/work/Python-3.6.6/
./configure --prefix=/usr/local/python36
make
sudo make install
sudo ln -s /usr/local/python36/bin/python3 /usr/bin/python3
sudo ln -s /usr/local/python36/bin/pip3 /usr/bin/pip3
安装 virtualenv
sudo pip3 install virtualenv
sudo pip3 install --upgrade pip
cd /home/jack/work
virtualenv -p /usr/bin/python3 py36_test
log为:
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr/local/python36'
New python executable in /home/jack/work/py36_test/bin/python3
Also creating executable in /home/jack/work/py36_test/bin/python
Installing setuptools, pip, wheel...done.
激活环境:
source /home/jack/work/py36_test/bin/activate
出现下面的效果:
(py36_test) [jack@ bin]$
(py36_test) [jack@ bin]$ pip install websockets
执行:
(py36_test) [jack@ py36_test]$ python server.py
server.py 内容为:
#!/usr/bin/env python
# WS server example for old Python versions
import asyncio
import websockets
@asyncio.coroutine
def hello(websocket, path):
name = yield from websocket.recv()
print("< {}".format(name))
greeting = "Hello {}!".format(name)
yield from websocket.send(greeting)
print("> {}".format(greeting))
start_server = websockets.serve(hello, '172.31.120.108', 8888)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
client.py的内容:
#!/usr/bin/env python
# WS client example for old Python versions
import asyncio
import websockets
@asyncio.coroutine
def hello():
websocket = yield from websockets.connect(
'ws://118.190.215.49:8888/')
try:
name = input("What's your name? ")
yield from websocket.send(name)
print("> {}".format(name))
greeting = yield from websocket.recv()
print("< {}".format(greeting))
finally:
yield from websocket.close()
asyncio.get_event_loop().run_until_complete(hello())
172.31.120.108 为个人测试用的阿里云服务器内部的地址
118.190.215.49 为个人测试用的阿里云的公网地址
服务端log:
扫描二维码关注公众号,回复:
2642168 查看本文章
(py36_test) [jack@ py36_test]$ python server.py
< abc
> Hello abc!
``
客户端log:
E:\python\python3\work\venv\Scripts\python.exe E:/python/python3/work/venv/client.py
What's your name? abc
> abc
< Hello abc!
Process finished with exit code 0
arduino 代码:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <Hash.h>
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
#define USE_SERIAL Serial
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED: {
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
webSocket.sendTXT("Connected");
}
break;
case WStype_TEXT:
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
hexdump(payload, length);
// send data to server
// webSocket.sendBIN(payload, length);
break;
}
}
void setup() {
// USE_SERIAL.begin(921600);
USE_SERIAL.begin(115200);
//Serial.setDebugOutput(true);
USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("LRTech", "lanrui2017");
//WiFi.disconnect();
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
// server address, port and URL
webSocket.begin("118.190.215.49", 8888, "/");
// event handler
webSocket.onEvent(webSocketEvent);
// use HTTP Basic Authorization this is optional remove if not needed
// webSocket.setAuthorization("user", "Password");
// try ever 5000 again if connection has failed
webSocket.setReconnectInterval(5000);
}
void loop() {
webSocket.loop();
}