2016-11-14 3 views
0

У меня есть этот код примера для клиента python websocket от here, и я столкнулся с проблемой ниже. Я редактировал этот код немного от исходного в нескольких местах, так как я получал в проблему:У объекта python websocket нет атрибута 'write_message'

  • Убран соединять метод и объединить его в конструктор. Я был получения 'self._ws_connection' не найденной проблемой.

  • Изменен метод WebSocketClient __init __(), чтобы принять 'url' и удалил аргумент ''. Я не уверен, что проходит '' означает.

задачи1:

Traceback (most recent call last): 
    File "websocketcli.py", line 138, in <module> 
    main() 
    File "websocketcli.py", line 129, in main 
    client.send('Hello world!') 
    File "websocketcli.py", line 49, in send 
    self._ws_connection.write_message(escape.utf8(json.dumps(data))) 
    File "https://stackoverflow.com/users/anjangam/pyvenv/venv/lib/python2.7/site-packages/tornado/websocket.py", line 970, in write_message 
    return self.protocol.write_message(message, binary) 
AttributeError: 'NoneType' object has no attribute 'write_message' 

Задача 2: Когда я удалить «*» из конструктора WbSocketClient, я получаю следующее сообщение об ошибке, почти такой же, как тот, встречающегося в задаче 1. Примечание: Я использую python версии 2.7.8.

Traceback (most recent call last): 
    File "websocketcli.py", line 138, in <module> 
    main() 
    File "websocketcli.py", line 129, in main 
    client.send('Hello world!') 
    File "websocketcli.py", line 52, in send 
    self._ws_connection.write_message(escape.utf8(json.dumps(data))) 
    File "https://stackoverflow.com/users/anjangam/pyvenv/venv/lib/python2.7/site-packages/tornado/websocket.py", line 970, in write_message 
    return self.protocol.write_message(message, binary) 
AttributeError: 'NoneType' object has no attribute 'write_message' 

код клиента:

from tornado import escape 
from tornado import gen 
from tornado import httpclient 
from tornado import httputil 
from tornado import ioloop 
from tornado import websocket 

import functools 
import json 
import time 


APPLICATION_JSON = 'application/json' 

DEFAULT_CONNECT_TIMEOUT = 60 
DEFAULT_REQUEST_TIMEOUT = 60 


class WebSocketClient(): 
"""Base for web socket clients. 
""" 
def __init__(self, connect_timeout=DEFAULT_CONNECT_TIMEOUT, 
      request_timeout=DEFAULT_REQUEST_TIMEOUT): 

    self.connect_timeout = connect_timeout 
    self.request_timeout = request_timeout 

def connect(self, url): 
    """Connect to the server. 
    :param str url: server URL. 
    """ 
    headers = httputil.HTTPHeaders({'Content-Type': APPLICATION_JSON}) 
    request = httpclient.HTTPRequest(url=url, 
            connect_timeout=self.connect_timeout, 
            request_timeout=self.request_timeout, 
            headers=headers) 
    self._ws_connection = websocket.WebSocketClientConnection(ioloop.IOLoop.current(), 
                request) 
    self._ws_connection.connect_future.add_done_callback(self._connect_callback) 
    print 'Connection: ', self._ws_connection 

    def send(self, data): 
     """Send message to the server 
     :param str data: message. 
     """ 
     if not self._ws_connection: 
      raise RuntimeError('Web socket connection is closed.') 

     self._ws_connection.write_message(escape.utf8(json.dumps(data))) 

    def close(self): 
     """Close connection. 
     """ 

     if not self._ws_connection: 
      raise RuntimeError('Web socket connection is already closed.') 

     self._ws_connection.close() 

    def _connect_callback(self, future): 
     if future.exception() is None: 
      self._ws_connection = future.result() 
      self._on_connection_success() 
      self._read_messages() 
     else: 
      self._on_connection_error(future.exception()) 

    @gen.coroutine 
    def _read_messages(self): 
     while True: 
      msg = yield self._ws_connection.read_message() 
      if msg is None: 
       self._on_connection_close() 
       break 

      self._on_message(msg) 

    def _on_message(self, msg): 
     """This is called when new message is available from the server. 
     :param str msg: server message. 
     """ 

     pass 

    def _on_connection_success(self): 
     """This is called on successful connection ot the server. 
     """ 

     pass 

    def _on_connection_close(self): 
     """This is called when server closed the connection. 
     """ 
     pass 

    def _on_connection_error(self, exception): 
     """This is called in case if connection to the server could 
     not established. 
     """ 

     pass 


class TestWebSocketClient(WebSocketClient): 

    def __init__(self, url): 
     WebSocketClient.__init__(self, url) 

    def _on_message(self, msg): 
     print(msg) 
     deadline = time.time() + 1 
     ioloop.IOLoop().instance().add_timeout(
      deadline, functools.partial(self.send, str(int(time.time())))) 

    def _on_connection_success(self): 
     print('Connected!') 
     self.send(str(int(time.time()))) 

    def _on_connection_close(self): 
     print('Connection closed!') 

    def _on_connection_error(self, exception): 
     print('Connection error: %s', exception) 


def main(): 
    client = TestWebSocketClient() 
    client.connect('ws://localhost:8888/ws') 
    client.send('Hello world!') 


    try: 
     ioloop.IOLoop.instance().start() 
    except KeyboardInterrupt: 
     client.close() 


if __name__ == '__main__': 
    main() 

ответ

1

оригинальный код ссылки работает нормально на питоне 3.5, но я подозреваю, что вы используете какую-то версию питона 2. Причина это разобьет бы из-за звездочку, о которой вы упомянули, которая не является особенностью python 2. PEP 3102 рассказывает об этом, но вкратце, что звездочка просто заставит вызовы функций передавать аргументы по ключевым словам, а не полагаться на порядок, в котором были поставлены параметры.

например, следующие функции:

# we can use positional arguments on this one 
def test(a, b): 
    print(a, b) 

# this will require keyword arguments to be supplied on function calls 
def test_keywords_args_required(*, a, b): 
    print(a, b) 

test(1, 2) # test can be called using positional arguments 
test_keywords_args_required(1, 2) # this will error out. Needs keywords args! 
test_keywords_args_required(a=1, b=2) # this will work! 

Если вы используете код первоначально подаваемый из этой ссылки, и удалите звездочку, код должен работать (подтвержденные питона 2.7).

Смежные вопросы