當前位置: 首頁>>代碼示例>>Python>>正文


Python socket.fileno方法代碼示例

本文整理匯總了Python中socket.fileno方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.fileno方法的具體用法?Python socket.fileno怎麽用?Python socket.fileno使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在socket的用法示例。


在下文中一共展示了socket.fileno方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: poll

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def poll(self, timeout=100):
        if self.use_poll:
            ready = self.poller.poll(timeout)
        else:
            ready = []
            if len(self.targets) > 0:
                (rlist, wlist, xlist) = select.select(
                    self.targets.keys(), [], [], timeout
                )
                ready = [(x, None) for x in rlist]

        for one_ready in ready:
            target = self.targets.get(one_ready[0].fileno(), None)
            dbg("Targets %s" % str(self.targets.keys()))
            if target:
                # dbg("get socket with fileno: %s" % str(one_ready[0].fileno()) +  " len: %s" % len(one_ready) + " selected: %s " % str(target.fileno()) )
                # update time
                target.do_read(one_ready[0]) 
開發者ID:lemariva,項目名稱:uPyEcho,代碼行數:20,代碼來源:main.py

示例2: do_read

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def do_read(self, socket):
        fileno = socket.fileno()
        dbg("Fileno %s" % fileno + " socket fileno %s" % self.socket.fileno())

        if fileno == self.socket.fileno():
            try:
                (client_socket, client_address) = self.socket.accept()
                self.poller.add(self, client_socket)
                self.client_sockets[client_socket.fileno()] = client_socket
            except Exception as e:
                dbg("################################## Socket busy! %s" % str(e))
        else:
            data, sender = self.client_sockets[fileno].recvfrom(4096)
            if not data:
                self.poller.remove(self, self.client_sockets[fileno])
                self.client_sockets[fileno].close()

            else:
                dbg("send response to socket!: %s" % str(fileno))
                self.handle_request(data, sender, self.client_sockets[fileno])
            gc.collect() 
開發者ID:lemariva,項目名稱:uPyEcho,代碼行數:23,代碼來源:main.py

示例3: connect

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def connect(self, host, port):
        self.__state = CONNECTION_STATE.DISCONNECTED
        self.__fileno = None
        self.__socket = socket.socket(_getAddrType(host), socket.SOCK_STREAM)
        self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.__sendBufferSize)
        self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.__recvBufferSize)
        self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.__socket.setblocking(0)
        self.__readBuffer = bytes()
        self.__writeBuffer = bytes()
        self.__lastReadTime = monotonicTime()

        try:
            self.__socket.connect((host, port))
        except socket.error as e:
            if e.errno not in (socket.errno.EINPROGRESS, socket.errno.EWOULDBLOCK):
                return False
        self.__fileno = self.__socket.fileno()
        self.__state = CONNECTION_STATE.CONNECTING
        self.__poller.subscribe(self.__fileno,
                                 self.__processConnection,
                                 POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
        return True 
開發者ID:bakwc,項目名稱:PySyncObj,代碼行數:25,代碼來源:tcp_connection.py

示例4: testBadSelectableTypes

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def testBadSelectableTypes(self):
        class Nope: pass

        class Almost1:
            def fileno(self):
                return 'fileno'

        class Almost2:
            def fileno(self):
                return 'fileno'

        # Test some known error conditions
        for bad_selectable in [None, 1, object(), Nope(), Almost1(), Almost2()]:
            try:
                timeout = 0 # Can't wait forever
                rfd, wfd, xfd = select.select([bad_selectable], [], [], timeout)
            except (TypeError, select.error), x:
                pass
            else:
                self.fail("Selecting on '%s' should have raised TypeError or select.error" % str(bad_selectable)) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:22,代碼來源:test_select.py

示例5: add

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def add(self, target, socket=None):
        if not socket:
            socket = target.sockets()
        if self.use_poll:
            self.poller.register(socket, select.POLLIN)
        # dbg("add device on fileno: %s" % socket.fileno() )
        self.targets[socket.fileno()] = target
        # dbg("size targets: %s" % len(self.targets)) 
開發者ID:lemariva,項目名稱:uPyEcho,代碼行數:10,代碼來源:main.py

示例6: remove

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def remove(self, target, socket=None):
        if not socket:
            socket = target.sockets()
        if self.use_poll:
            self.poller.unregister(socket)
        # dbg("remove device on fileno: %s" % socket.fileno() )
        gc.collect() 
開發者ID:lemariva,項目名稱:uPyEcho,代碼行數:9,代碼來源:main.py

示例7: fileno

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def fileno(self):
        return self.socket.fileno() 
開發者ID:lemariva,項目名稱:uPyEcho,代碼行數:4,代碼來源:main.py

示例8: add

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def add(self, socket = None):
        if not socket:
            return
        self.poller.register(socket, select.POLLIN)
        self.targets[socket.fileno()] = socket 
開發者ID:lemariva,項目名稱:uPyPortal,代碼行數:7,代碼來源:dns.py

示例9: remove

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def remove(self, socket = None):
        if not socket:
            return
        self.poller.unregister(socket)
        del(self.targets[socket.fileno()]) 
開發者ID:lemariva,項目名稱:uPyPortal,代碼行數:7,代碼來源:dns.py

示例10: poll

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def poll(self, ip, timeout = 100):
        ready = self.poller.poll(timeout)
        for one_ready in ready:
            target = self.targets.get(one_ready[0].fileno(), None)
            if target:
                try:
                    data, sender = target.recvfrom(1024)
                    p = DNSQuery(data)
                    if any(word in p.domain for word in config.DNS_ANSWERS):
                        target.sendto(p.answer(ip), sender)
                        dbg('Replying: {:s} -> {:s}'.format(p.domain, ip))
                    #else:
                    #    dbg('Avoid responding: {:s} -> {:s}'.format(p.domain, ip))
                except Exception as e:
                    dbg('Exception occurs: ' + str(e)) 
開發者ID:lemariva,項目名稱:uPyPortal,代碼行數:17,代碼來源:dns.py

示例11: __init__

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def __init__(self, poller, onMessageReceived = None, onConnected = None, onDisconnected = None,
                 socket=None, timeout=10.0, sendBufferSize = 2 ** 13, recvBufferSize = 2 ** 13):

        self.sendRandKey = None
        self.recvRandKey = None
        self.encryptor = None

        self.__socket = socket
        self.__readBuffer = bytes()
        self.__writeBuffer = bytes()
        self.__lastReadTime = monotonicTime()
        self.__timeout = timeout
        self.__poller = poller
        if socket is not None:
            self.__socket = socket
            self.__fileno = socket.fileno()
            self.__state = CONNECTION_STATE.CONNECTED
            self.__poller.subscribe(self.__fileno,
                                     self.__processConnection,
                                     POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
        else:
            self.__state = CONNECTION_STATE.DISCONNECTED
            self.__fileno = None
            self.__socket = None

        self.__onMessageReceived = onMessageReceived
        self.__onConnected = onConnected
        self.__onDisconnected = onDisconnected
        self.__sendBufferSize = sendBufferSize
        self.__recvBufferSize = recvBufferSize 
開發者ID:bakwc,項目名稱:PySyncObj,代碼行數:32,代碼來源:tcp_connection.py

示例12: fileno

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def fileno(self):
        return self.__fileno 
開發者ID:bakwc,項目名稱:PySyncObj,代碼行數:4,代碼來源:tcp_connection.py

示例13: bind_and_listen_on_posix_socket

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def bind_and_listen_on_posix_socket(socket_name, accept_callback):
    """
    :param accept_callback: Called with `PosixSocketConnection` when a new
        connection is established.
    """
    assert socket_name is None or isinstance(socket_name, six.text_type)
    assert callable(accept_callback)

    # Py2 uses 0027 and Py3 uses 0o027, but both know
    # how to create the right value from the string '0027'.
    old_umask = os.umask(int('0027', 8))

    # Bind socket.
    socket_name, socket = _bind_posix_socket(socket_name)

    _ = os.umask(old_umask)

    # Listen on socket.
    socket.listen(0)

    def _accept_cb():
        connection, client_address = socket.accept()
        # Note: We don't have to put this socket in non blocking mode.
        #       This can cause crashes when sending big packets on OS X.

        posix_connection = PosixSocketConnection(connection)

        accept_callback(posix_connection)

    get_event_loop().add_reader(socket.fileno(), _accept_cb)

    logger.info('Listening on %r.' % socket_name)
    return socket_name 
開發者ID:prompt-toolkit,項目名稱:pymux,代碼行數:35,代碼來源:posix.py

示例14: __init__

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def __init__(self, socket):
        self.socket = socket
        self._fd = socket.fileno()
        self._recv_buffer = b'' 
開發者ID:prompt-toolkit,項目名稱:pymux,代碼行數:6,代碼來源:posix.py

示例15: _read_chunk_from_socket

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fileno [as 別名]
def _read_chunk_from_socket(socket):
    """
    (coroutine)
    Turn socket reading into coroutine.
    """
    fd = socket.fileno()
    f = Future()

    def read_callback():
        get_event_loop().remove_reader(fd)

        # Read next chunk.
        try:
            data = socket.recv(1024)
        except OSError as e:
            # On OSX, when we try to create a new window by typing "pymux
            # new-window" in a centain pane, very often we get the following
            # error: "OSError: [Errno 9] Bad file descriptor."
            # This doesn't seem very harmful, and we can just try again.
            logger.warning('Got OSError while reading data from client: %s. '
                           'Trying again.', e)
            f.set_result('')
            return

        if data:
            f.set_result(data)
        else:
            f.set_exception(BrokenPipeError)

    get_event_loop().add_reader(fd, read_callback)

    return f 
開發者ID:prompt-toolkit,項目名稱:pymux,代碼行數:34,代碼來源:posix.py


注:本文中的socket.fileno方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。