【问题标题】:Python script to connect to socket.io Server created using Nodejs用于连接到使用 Nodejs 创建的 socket.io 服务器的 Python 脚本
【发布时间】:2015-06-22 10:35:11
【问题描述】:

我使用 nodejs 在http://localhost:8090 托管了一个 HTTP 服务器。

app.js 文件

var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');

var server = http.createServer(function(req, res) {
        res.writeHead(200, { 'Content-type': 'text/html'});
        res.end(fs.readFileSync(__dirname + '/index.html'));
        }).listen(8090, function() {
            console.log('Listening at: http://localhost:8090');
            });

socketio.listen(server).on('connection', function (socket) {

        socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
        });        
    });
});

现在我编写了一个 python 脚本来使用套接字编程连接到这个服务器。

client.py 文件

#!/usr/bin/python           # This is client.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = '127.0.0.1'     # Get local machine name
port = 8090                # Reserve a port for your service.

s.connect((host, port))
s.send('Thanks for connecting.')
s.close    

当我运行 client.py 文件时,我无法在服务器端收到“感谢连接”消息。

我无法理解我哪里出错了。

【问题讨论】:

    标签: python node.js sockets socket.io server


    【解决方案1】:

    Socket.io 使用创建 WebSocket 服务器。在 python 客户端中,您通过原始套接字发送纯文本。你需要的是socket.io client library for python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多