pywebsocket不起作用

时间:2012-12-15 23:56:40

标签: apache websocket mod-pywebsocket

这就是我安装mod_pywebsocket作为apache扩展

的方式
root@senior:/var/www# wget http://pywebsocket.googlecode.com/files/mod_pywebsocket-0.7.8.tar.gz
root@senior:/var/www# tar xvf mod_pywebsocket-0.7.8.tar.gz
root@senior:/var/www# cd pywebsocket-0.7.8/src/
root@senior:/var/www/pywebsocket-0.7.8/src# py:/./setpuup.py build
running build
running build_py
root@senior:/var/www/pywebsocket-0.7.8/src# python ./setup.py install
running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/local/lib/python2.6/dist-packages/mod_pywebsocket-0.7.8.egg-info
Writing /usr/local/lib/python2.6/dist-packages/mod_pywebsocket-0.7.8.egg-info    
root@senior:/var/www/pywebsocket-0.7.8/src# mkdir /var/www/_wsh/
root@senior:/var/www/pywebsocket-0.7.8/src# cp example/* /var/www/_wsh/
root@senior:/var/www/pywebsocket-0.7.8/src# cd /var/www/_wsh/
root@senior:/var/www/_wsh# chmod a+x *.py
root@senior:/usr/local/lib/python2.6/dist-packages/mod_pywebsocket# cd /usr/local/lib/python2.6/dist-packages/mod_pywebsocket
root@senior:/usr/local/lib/python2.6/dist-packages/mod_pywebsocket# chmod a+rx *.py

这是我在 httpd.conf

中插入的文字
<IfModule python_module>
  PythonPath "sys.path+['/usr/local/lib/python2.6/dist-packages']"
  PythonOption mod_pywebsocket.handler_root /var/www/_wsh/
  PythonOption mod_pywebsocket.handler_scan /var/www/_wsh/
  #PythonOption mod_pywebsocket.allow_draft75 On
  #<Location /var/www/_vsh>
  #  PythonHeaderParserHandler mod_pywebsocket.headerparserhandler
  #</Location>
</IfModule>

重新启动apaceh后,我正在尝试使用pywebsocket目录中的console.html代码(复制在/ var / www / _wsh中)。但它不起作用,我无法打开连接。

当我尝试运行示例时,我得到了这个

root@senior:/usr# /var/www/_wsh/echo_client.py
Traceback (most recent call last):
  File "/var/www/_wsh/echo_client.py", line 995, in <module>
    main()
  File "/var/www/_wsh/echo_client.py", line 991, in main
    EchoClient(options).run()
  File "/var/www/_wsh/echo_client.py", line 840, in run
    self._handshake.handshake()
  File "/var/www/_wsh/echo_client.py", line 385, in handshake
    'Expected HTTP status code 101 but found %r' % status_code)
__main__.ClientHandshakeError: Expected HTTP status code 101 but found '200'
你可以帮我找一下问题所在吗?谢谢

2 个答案:

答案 0 :(得分:1)

将它作为独立服务器运行要容易得多。你试过了吗?

"""Standalone WebSocket server.

Use this file to launch pywebsocket without Apache HTTP Server.


BASIC USAGE

Go to the src directory and run

  $ python mod_pywebsocket/standalone.py [-p <ws_port>]
                                         [-w <websock_handlers>]
                                         [-d <document_root>]

<ws_port> is the port number to use for ws:// connection.

<document_root> is the path to the root directory of HTML files.

<websock_handlers> is the path to the root directory of WebSocket handlers.
If not specified, <document_root> will be used. See __init__.py (or
run $ pydoc mod_pywebsocket) for how to write WebSocket handlers.

For more detail and other options, run

  $ python mod_pywebsocket/standalone.py --help

or see _build_option_parser method below.

For trouble shooting, adding "--log_level debug" might help you.

<强>更新

请尝试使用this link解决问题。

答案 1 :(得分:1)

在阅读了很多关于如何设置pywebsockets的教程之后,我结束了以下配置:

LogLevel debug
<IfModule python_module>
  Timeout 0
  #This is path where I have copied pywebsocket
  PythonPath "sys.path+['/usr/local/lib/python2.6/dist-packages']"
  #This is path where I have my websocket connection handlers (files that responds to requests, I have there example files copied too)
  PythonOption mod_pywebsocket.handler_root /var/www/websockets/
  PythonOption mod_pywebsocket.handler_scan /var/www/websockets/
  PythonHeaderParserHandler mod_pywebsocket.headerparserhandler
</IfModule>

以下列出了这些路径,以便更好地了解其中的内容:

root@senior:/var/www/websockets# ls /usr/local/lib/python2.6/dist-packages/
mod_pywebsocket  mod_pywebsocket-0.7.8.egg-info
root@senior:/var/www/websockets# ls /usr/local/lib/python2.6/dist-packages/mod_pywebsocket
common.py       handshake                __init__.pyc        mux.pyc             _stream_hixie75.pyc  util.pyc
common.pyc      headerparserhandler.py   memorizingfile.py   standalone.py       _stream_hybi.py
dispatch.py     headerparserhandler.pyc  memorizingfile.pyc  standalone.pyc      _stream_hybi.pyc
dispatch.pyc    http_header_util.py      msgutil.py          _stream_base.py     stream.py
extensions.py   http_header_util.pyc     msgutil.pyc         _stream_base.pyc    stream.pyc
extensions.pyc  __init__.py              mux.py              _stream_hixie75.py  util.py
root@senior:/var/www/websockets#
root@senior:/var/www/websockets# ls .
bench_wsh.py  console.html    echo_wsh.py      origin_check_wsh.py
close_wsh.py  echo_client.py  handler_map.txt  pywebsocket.conf

我还必须编辑apache的mod_reqtimeout以保持连接活动(请参阅此问题以获取更多信息Why would a websocket created with pywebsocket be closed automatically?