将JS客户端与Python服务器连接 [英] Connect JS client with Python server

查看:304
本文介绍了将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服务器,然后您可以在节点中使用类似于以下的代码:

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服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆