当前位置: 首页>>代码示例>>Python>>正文


Python socket.setblocking方法代码示例

本文整理汇总了Python中socket.socket.setblocking方法的典型用法代码示例。如果您正苦于以下问题:Python socket.setblocking方法的具体用法?Python socket.setblocking怎么用?Python socket.setblocking使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在socket.socket的用法示例。


在下文中一共展示了socket.setblocking方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _is_client_request_ssl

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setblocking [as 别名]
    def _is_client_request_ssl(cls, socket_: socket.socket) -> bool:
        while True:
            original_timeout = socket_.gettimeout()
            socket_.setblocking(False)

            try:
                data = socket_.recv(3, socket.MSG_PEEK)
            except OSError as error:
                if error.errno in (errno.EWOULDBLOCK, errno.EAGAIN):
                    yield from asyncio.sleep(0.01)
                else:
                    raise
            else:
                break
            finally:
                socket_.settimeout(original_timeout)

        _logger.debug('peeked data %s', data)
        if all(ord('A') <= char_code <= ord('Z') for char_code in data):
            return False
        else:
            return True
开发者ID:Super-Rad,项目名称:wpull,代码行数:24,代码来源:server.py

示例2: _InProgressSocketManager

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setblocking [as 别名]
class _InProgressSocketManager(object):
    """
    This class does not actually work right now. It's something that I'm
    writing that will some day replace InputThread and OutputThread and allow
    Autobus to function using only a single thread for each Bus instance. So
    it's a work in progress, and it doesn't actually work right now.
    """
    def __init__(self):
        # Set up the interrupt socket
        interrupt_server = Socket()
        interrupt_server.bind(("localhost", 0))
        interrupt_server.listen(1)
        self.interrupt_writer = Socket()
        self.interrupt_writer.setblocking(False)
        self.interrupt_writer.connect("localhost", interrupt_server.getsockname()[1])
        self.interrupt_reader = interrupt_server.accept()
        interrupt_server.shutdown(SHUT_RDWR)
        interrupt_server.close()
        self.interrupt_reader.setblocking(False)
        self.interrupt_writer.setblocking(False)
    
    def loop(self):
        pass
    
    def run_sync(self, function, timeout=None):
        q = Queue()
        @self.run_async
        def new_function():
            q.put(function())
        try:
            return q.get(timeout=timeout)
        except Empty:
            raise exceptions.TimeoutException
    
    def run_async(self, function):
        pass
    
    def interrupt(self):
        self.interrupt_writer.send(0)
开发者ID:giflw,项目名称:afn-tools,代码行数:41,代码来源:net.py

示例3: __init__

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setblocking [as 别名]
class GoClient:

    def __init__(self, IP='127.0.0.1', port=5005):
        self.root = tk.Tk()
        self.IP = IP
        self.port = port
        self.players = []

    def start_game(self, player, size):
        self.players.append(player)
        self.board = Board(size)
        print 'beginning game as player %d' % player
        self.make_display()
        self.gui.start_game()

    def made_move(self, i, j):
        self.board.place_piece(i, j)
        self.gui.made_move()

    def passed_turn(self):
        self.board.pass_turn()
        self.gui.passed_turn()

    def game_over(self, score1, score2):
        self.gui.set_message('Game Over, Black: %d White: %d' % (score1, score2))
        self.board.gameover = True

    def on_click(self, i, j):
        if self.board.turn in self.players:
            self.send('MAKEMOVE %d %d' % (i, j))
            # self.receive('MADEMOVE %d %d' % (i, j))

    def on_quit(self):
        send('QUIT')
        self.gui.parent.destroy()

    def on_pass(self):
        if self.board.turn in self.players:
            self.send('PASSTURN')

    def run(self):
        # self.start_game(1)
        # self.start_game(2)
        self.connect_to_server()
        self.root.title('Python Online Five-In-A-Row')
        # root.resizable(0,0)
        self.root.mainloop()
        # print 'received data:', data

    def make_display(self):
        self.gui = BoardGui(parent=self.root, board=self.board, players=self.players)
        self.gui.on_click.append(self.on_click)
        self.gui.on_pass.append(self.on_pass)
        self.gui.pack(side='top', fill='both', expand='true', padx=4, pady=4)
       
    def receive(self, data):
        print 'receiving [%s]' % data
        data = data.split()
        if not data:
            return
        message = data[0]
        if message == 'BEGINGAME':
            self.start_game(int(data[1]), int(data[2]))
        elif message == 'MADEMOVE':
            i, j = map(int, data[1:3])
            self.made_move(i, j)
        elif message == 'PASSEDTURN':
            self.passed_turn()
        elif message == 'GAMEOVER':
            a, b = map(int, data[1:3])
            self.game_over(a, b)

    def send(self, data):
        print 'sending %s' % data
        self.skt.send(data)

    def connect_to_server(self):
        BUFFER_SIZE = 1024

        self.skt = Socket(AF_INET, SOCK_STREAM)
        self.skt.connect((self.IP, self.port))
        self.skt.setblocking(0)
        def listen():
            try:
                data = self.skt.recv(BUFFER_SIZE)
            except SocketError:
                pass
            else:
                if not data:
                    return
                for line in data.split('\n'):
                    self.receive(line)
            self.root.after(500, listen)

        listen()
开发者ID:stevenhao,项目名称:wow,代码行数:97,代码来源:client.py

示例4: __init__

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setblocking [as 别名]
class GoBotClient:

    def __init__(self, IP='127.0.0.1', port=5005):
        self.IP = IP
        self.port = port
        self.player = None
        self.turn = 2

    def start_game(self, player, size):
        self.player = player
        print 'beginning game as player %d' % player
        self.GoBot = GoBot(0 if player == 2 else 1)
        
        if player == 2:
            self.make_move('pass')

    def made_move(self, i, j):
        self.turn = 1 if self.turn == 2 else 2

    def passed_turn(self):
        self.turn = 1 if self.turn == 2 else 2

    def make_move(self, move):
        if self.turn == self.player:
            if move != 'pass':
                gobot_move = self.GoBot.make_move((move[1], move[0]))
            else:
                gobot_move = self.GoBot.make_move('pass')
            self.send('MAKEMOVE %d %d' % (gobot_move[1], gobot_move[0]))

    def run(self):
        self.connect_to_server()
        while True:
            if self.listen():
                print "Disconnected from server\n"
                return
       
    def receive(self, data):
        print 'receiving [%s]' % data
        data = data.split()
        if not data:
            return
        message = data[0]
        if message == 'BEGINGAME':
            self.start_game(int(data[1]), int(data[2]))
        elif message == 'MADEMOVE':
            i, j = map(int, data[1:3])
            self.made_move(i, j)
            if self.turn == self.player:
                self.make_move((i, j))
        elif message == 'PASSEDTURN':
            self.passed_turn()
            if self.turn == self.player:
                self.make_move('pass')
        elif message == 'GAMEOVER':
            a, b = map(int, data[1:3])
            self.game_over(a, b)
        elif message == 'FINISH':
            return True

    def send(self, data):
        print 'sending %s' % data
        self.skt.send(data)

    def connect_to_server(self):
        BUFFER_SIZE = 1024

        self.skt = Socket(AF_INET, SOCK_STREAM)
        self.skt.connect((self.IP, self.port))
        self.skt.setblocking(0)
    
    def listen(self):
        BUFFER_SIZE = 1024

        try:
            data = self.skt.recv(BUFFER_SIZE)
        except SocketError:
            pass
        else:
            if not data:
                return
            for line in data.split('\n'):
                if self.receive(line):
                    return True
开发者ID:stevenhao,项目名称:yeah,代码行数:86,代码来源:gobot_client.py


注:本文中的socket.socket.setblocking方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。