English 中文(简体)
图一
原标题:python asyncore keep track of clients

我写了一个简单的袖珍服务器,我希望跟踪客户状况(真实和 st)。 每次处理时,我都不了解有关该客户的任何情况。 如果我知道客户身份或某件事,会有所帮助。 这里我迄今为止所做的事情:

import asyncore
import socket

class EchoHandler(asyncore.dispatcher_with_send):

    def handle_read(self):
        data = self.recv(8192)
        self.send(data)

class EchoServer(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)

    def handle_accept(self):
        pair = self.accept()
        if pair is None:
            pass
        else:
            sock, addr = pair
            print  Incoming connection from %s  % repr(addr)
            handler = EchoHandler(sock)

server = EchoServer( localhost , 8080)
asyncore.loop()
最佳回答

你在接到<条码>查询时确实知道客户的身份。 回归的图象是IP,是该客户的一个独一无二的数字,在你希望向该客户发送数据时可以使用。 如果你想保留客户名单,你就应考虑一名字典,在接收新链接时,储存客户的信息。

If you wanted that information passed on to the handle_read function, your code would look something like below:

class EchoHandler(asyncore.dispatcher_with_send):
    def setAddr(self, addr):
        self.addr = addr

    def handle_read(self):
        data = self.recv(8192)
        print  %s from %s  % (data, self.addr)
        self.send(data)

class EchoServer(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)

    def handle_accept(self):
        pair = self.accept()
        if pair is None:
            pass
        else:
            sock, addr = pair
            print  Incoming connection from %s  % repr(addr)
            handler = EchoHandler(sock)
            handler.setAddr(addr) #Set the address of the sender in the EchoHandler

简单地将客户的地址寄给EchoHandler,现在你们知道它来自哪里。 希望这一帮助/工作!

问题回答

The only "id" that you can get from sockets directly is the reference to the socket and the sender s address. Everything else is based on the data that you receive - your client could e.g. send its id as the first 16 bytes, anything after that would be data. This means effectively implementing a protocol that fulfills your requirements.

更方便、更妥善地解决认证问题的办法,是使用一些图书馆,例如:sl,载于python标准图书馆。 <代码>sl 图书馆提供加密和认证机制,其形式可以很容易地用在书状上。 如果你需要可靠的认证,我强烈建议利用现有解决办法。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签