使用select()时如何发送流槽插槽?

时间:2016-11-29 15:30:55

标签: python python-2.7 sockets polling

经过长时间的研究和测试,我终于在这里问了。

我的脚本必须处理多个客户端连接,同时必须从另一个套接字获取和发送流。

最后,我已经能够使其工作,但仅限于一个用户。该用户连接到套接字,脚本连接到另一个套接字,然后将流返回到客户端。 该脚本运行良好,但有一些硬性限制: - 它将流发送到客户端,但是, - 即使套接字处于非阻塞模式,我认为在另一个套接字内调用套接字是它在阻塞模式下做出反应的主要原因(因为其中一个不断发送数据?)

顺便说一句,我认为select()方法可以让我做我想做的事,但我不清楚如何理解。

这是服务器代码taht适用于一个客户端,但正在阻止

#!/usr/bin/env python
# coding: utf-8 

from __future__ import print_function

import sys, time, base64, socket

server_ip = 'XX.XX.XX.XX'

def caster_connect(connected_client, address):
    username = 'XXXXXXX'
    password = 'XXXXXXXXX'
    host = 'XX.XX.XX.XX'
    port = 2102
    pwd = base64.b64encode("{}:{}".format(username, password).encode('ascii'))
    pwd = pwd.decode('ascii')
    u_message = ''
    stream_type = 'CMRp'

    header = \
    "GET /" + str(stream_type) + " HTTP/1.1\r\n" +\
    "Host " + str(host) + "\r\n" +\
    "Ntrip-Version: Ntrip/1.0\r\n" +\
    "User-Agent: my_script.py/0.1\r\n" +\
    "Accept: */*\r\n" +\
    "Authorization: Basic {}\r\n\r\n".format(pwd) +\
    "Connection: close\r\n"

    print("Connecting to caster...\n")

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host,int(port)))
    s.send(header.encode('ascii'))
    print("Waiting answer from caster...\n")

    while True:
        try:
            data = s.recv(2048)

            connected_client.send(data)
            print("Sending data from caster at %s" % time.time())
            sys.stdout.flush()
        # On any error, close sockets
        except socket.error, e:
            print("No data received from caster : %s" % e)
            print("Close client connection at %s" % format(address))
            s.close()
            break

    return

#----------------

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((server_ip, 5680))
sock.settimeout(3)

try:
    while True:
        try:
            sock.listen(5)
            client, address = sock.accept()
            print ("%s connected" % format(address) )

            msg = client.recv(4096)
        except socket.timeout, e:
            err = e.args[0]
            if err == 'timed out':
                print("Timed out, retry later")
                continue
            else:
                print(socket.error)
                sock.close()
        except socket.error:
            print(socket.error)
            sock.close()
        else:
            if len(msg) == 0:
                print("Shutdown on client end")
                sock.close()
            else:
                print(msg)
                caster_response = caster_connect(client, address)
        sys.stdout.flush()

    print("Close")
    client.close()
    sock.close()`enter code here`
except KeyboardInterrupt:
    print("W: Keyboard interrupt, closing socket")
finally:
    sock.close()

这是我发现处理select()

的代码
#!/usr/bin/env python
# coding: utf-8 

import select, socket, sys, Queue

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
server.bind(('XX.XX.XX.XX', 64000))
server.listen(5)
inputs = [server]
outputs = []
message_queues = {}

while inputs:
    readable, writable, exceptional = select.select(
        inputs, outputs, inputs)

    for s in readable:
        if s is server:
            connection, client_address = s.accept()
            print("New connection from %s" % client_address)
            connection.setblocking(0)
            inputs.append(connection)
            message_queues[connection] = Queue.Queue()
        else:
            data = s.recv(1024)
            print("Data received : %s" % data)
            if data:
                message_queues[s].put(data)
                if s not in outputs:
                    outputs.append(s)
            else:
                if s in outputs:
                    outputs.remove(s)
                inputs.remove(s)
                s.close()
                del message_queues[s]

    for s in writable:
        try:
            next_msg = message_queues[s].get_nowait()
            print("Next msg : %s" % next_msg)
        except Queue.Empty:
            outputs.remove(s)
        else:
            s.send(next_msg)

    for s in exceptional:
        inputs.remove(s)
        if s in outputs:
            outputs.remove(s)
        s.close()
        del message_queues[s]

在此代码中(在this page处找到)我没有进行更改,因为我不知道如何处理此问题。

也许通过创建另一个只处理流部分的服务器脚本,所以主脚本将充当客户端的服务器,但作为流部分的客户端?

0 个答案:

没有答案