本文介绍了连接 JS 客户端和 Python 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 JS 和 Python 比较陌生,所以这可能是一个初学者问题.我正在尝试将字符串从 JS 客户端发送到 Python 服务器(然后将该字符串发送到另一个 Python 客户端).

I'm relatively new to JS and Python, so this is probably a beginners question. I'm trying to send a string from a JS client to Python Server (and then send the string to another Python client).

这是我的代码:

JS 客户端:

    var socket = io.connect('http://127.0.0.1:8484');
    socket.send('lalala');

Python 服务器:

Python server:

HOST = '127.0.0.1'
PORT = 8484
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(2)

#JS
conn1, addr1 = s.accept()
print 'Connected by', addr1
#PY
conn2, addr2 = s.accept()
print 'Connected by', addr2

while 1:
    try:
        data = conn1.recv(1024)
    except socket.error:
        print ''
    if data:
        print data.decode('utf-8')
        conn2.send('data')

Python 客户端:

Python client:

def __init__(self): #inicializacion
    self.comando = '0'
    self.HOST = '127.0.0.1'
    self.PORT = 8484
    self.cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    fcntl.fcntl(self.cliente, fcntl.F_SETFL, os.O_NONBLOCK)

def activate(self):
    print "Plugin de envio en marcha."

def deactivate(self):
    print "Plugin de envio off."

def __call__(self, sample):
    self.cliente.connect((self.HOST, self.PORT))
    try:
        self.comando = self.cliente.recv(1024).decode('utf-8')

    except socket.error, e:
        self.comando = '0'

    print "******************"
    print self.comando
    print "******************"

我从python服务器向python客户端发送一个随机字符串没有问题,但是我无法从JS客户端接收.

I have no problem sending a random string from python server to python client, but I can't receive from the JS client.

当我运行服务器和客户端时,连接没有问题:

When I run the server and the clients there are no problems with the connection:

Connected by ('127.0.0.1', 52602)
Connected by ('127.0.0.1', 52603)

但是,例如,这是我从 JS 收到的:

But, for example, this is what I receive from the JS:

GET /socket.io/1/?t=1472322502274 HTTP/1.1
Host: 127.0.0.1:8484
Connection: keep-alive
Origin: http://127.0.0.1:8880
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Accept: */*
Referer: http://127.0.0.1:8880/normal.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: es-ES,es;q=0.8
Cookie: io=e7675566ceab4aa1991d55cd72c8075c

但我想要字符串 'lalala'.

But I want the string 'lalala'.

有什么想法吗?

(谢谢!)

推荐答案

如果你想连接python的TCP服务器,那么你可以在node中使用类似如下的代码:

If you want to connect to the python TCP server, then you can use a code similar to the following in node:

var net = require('net');

var client = new net.Socket();
client.connect(8484, '127.0.0.1', function() {
    console.log('Connected');
    client.write('Hello, server! Love, Client.');
});

client.on('data', function(data) {
    console.log('Received: ' + data);
    client.destroy(); // kill client after server's response
});

由您的 python 代码启动的服务器不侦听原始 TCP 连接.如果你阅读了 HTTP 协议,你就会明白为什么当你的 js代码发送了那个字符串.

The server started by your python code doesn't listens for raw TCP connections. If you read up on HTTP protocol you will understand why you saw those strings when your js code sent that string.

这篇关于连接 JS 客户端和 Python 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:47