centos 7 安装 python3.6 阿里云 websocket 通信

http://www.websocket.org/echo.html

命令:

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:

(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();
}

网页测试方法

http://www.websocket.org/echo.html
ws://118.190.215.49:8888/

这里写图片描述

yangsuite /home/user/.local/lib/python3.6/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6. from cryptography.hazmat.backends import default_backend ERROR:root:Error in loading YANG Suite app "yangsuite-gnmi": The &#39;yang.connector&#39; distribution was not found and is required by the application ERROR:root:Error in loading YANG Suite app "yangsuite-grpc-telemetry": The &#39;yang.connector&#39; distribution was not found and is required by the application Removing database for upgrade. Traceback (most recent call last): File "/home/user/.local/bin/yangsuite", line 8, in <module> sys.exit(main()) File "/home/user/.local/lib/python3.6/site-packages/yangsuite/application.py", line 224, in main execute_from_command_line([&#39;manage&#39;, &#39;migrate&#39;, &#39;--no-color&#39;]) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/home/user/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 361, in execute self.check() File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check include_deployment_checks=include_deployment_checks, File "/home/user/.local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 65, in _run_checks issues.extend(super()._run_checks(**kwargs)) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 377, in _run_checks return checks.run_checks(**kwargs) File "/home/user/.local/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks new_errors = check(app_configs=app_configs) File "/home/user/.local/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config return check_resolver(resolver) File "/home/user/.local/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver return check_method() File "/home/user/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 403, in check for pattern in self.url_patterns: File "/home/user/.local/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/user/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 588, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/home/user/.local/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/user/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 581, in urlconf_module return import_module(self.urlconf_name) File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/user/.local/lib/python3.6/site-packages/yangsuite/urls.py", line 95, in <module> include(ac.name + &#39;.urls&#39;))) File "/home/user/.local/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include urlconf_module = import_module(urlconf_module) File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/user/.local/lib/python3.6/site-packages/yscoverage/urls.py", line 3, in <module> from . import views, consumers File "/home/user/.local/lib/python3.6/site-packages/yscoverage/views.py", line 22, in <module> from yscoverage.dataset import ALL_COLUMNS, get_module_dataset File "/home/user/.local/lib/python3.6/site-packages/yscoverage/dataset.py", line 9, in <module> from channels.layers import get_channel_layer ModuleNotFoundError: No module named &#39;channels&#39;
最新发布
11-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值