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


Python socket.accept函数代码示例

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


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

示例1: run

 def run(socket):
     socket.bind((address, port))
     socket.listen()
     launched_event.set()
     try:
         socket.accept(timeout=self.DEFAULT_TIMEOUT)
         self.end_event.wait()
         socket.free()
     except Exception, e:
         traceback.print_exc(e)
         self.network.close()
开发者ID:ealmansi,项目名称:tdc-tp3,代码行数:11,代码来源:base.py

示例2: loop

 def loop(self):
     while not self.stop or self.clientManager.clients:            
         read = []
         write = []
         error = []
         
         for client in self.clientManager.clients:
             if client.shouldSelectForRead:
                 read.append(client)
             
             if client.shouldSelectForWrite:
                 write.append(client)
             
             if client.shouldSelectForError:
                 error.append(client)
         
         if not self.stop:
             # Add the server only if we're not stopping it
             read.append(self.ssocket)
         
         readable, writable, errored = select(read, write, error)
         
         for socket in readable:
             if socket is self.ssocket:
                 self.clientManager.clientJoined(*socket.accept())
             else:
                 if not socket.read():
                     # Client left
                     self.clientManager.clientLeft(socket)
开发者ID:aspyct,项目名称:Cenchria,代码行数:29,代码来源:server.py

示例3: run

 def run(self):
     """main loop : accepts incoming connections and receive messages"""
     
     self.log("run")
     
     while True:
         sockets_to_read = select.select([self.server_socket] + self.connected_clients + list(self.logged_clients.values()), [], [])[0]            
         
         self.log("sockets_to_read : {}".format(sockets_to_read))
         
         for socket in sockets_to_read:                 
             if socket is self.server_socket:
                 self.log("new connection")
                 # accept the new connection
                 client_socket = socket.accept()[0]
                 # add it to the client sockets list
                 self.connected_clients.append(client_socket)     
             else:
                 self.log("new message")          
                 try:
                     message = self.read_message(socket)                            
                     if message:
                         self.log(message.decode(ENCODING))     
                         self.handle_message(message, socket)                   
                     else:                                     
                         # the client has properly disconnected                            
                         self.close_client(socket)
                 except ConnectionResetError:
                     # the client has abruptly disconnected 
                     self.error("connection reset for socket {}".format(socket))
                     self.close_client(socket)
             
     # clean up
     self.server_socket.close()            
开发者ID:bastoche,项目名称:ChatServer,代码行数:34,代码来源:chatserver.py

示例4: serve

  def serve(self):
    while True:
      try:
        inputready,outputready,exceptready = select.select(self.servers.keys(), [], [], 0)
      except e:
        print e
        break

      for socket in inputready:
        if socket == self.server:
          client, address = socket.accept()
          self.recvConnection(client, address)
        else:
          try:
            data = socket.recv(2048)
            if data:
              if self.servers[socket].ready:
                self.recvData(socket, data)
              else:
                self.recvHandshake(socket, data)
            else:
              self.dropConnection(socket)
          except:
            self.dropConnection(socket)

      time.sleep(0.05)

      for client in self.servers.keys():
        if client != self.server:
          frame = self.servers[client].popFrameBuffer()
          if frame != None:
            self.handler[frame.opcode](client, frame)

    self.server.close()
开发者ID:vukicevic,项目名称:echo,代码行数:34,代码来源:se.py

示例5: main

def main():
    print("[HTT] Start server")
    socket = HTTPSocket()
    print("[HTT] bind 8080")
    socket.bind(8080)

    # get connection
    conn, addr = socket.accept()

    while conn is not None:

        thread = HTTPMainThread(conn, 1024, addr[0], addr[1])

        thread.run()

        conn, addr = socket.accept()
开发者ID:Luavis,项目名称:htt.py,代码行数:16,代码来源:htt.py

示例6: serve

 def serve(self):
     self._running = True
     socket = evtapi.tcp_listener((self.host,self.port))
     while self._running:
       client, _ = socket.accept()
       Dispatcher(client,self.mapper).dispatch()
     socket.close()
开发者ID:mbreese,项目名称:proxylet,代码行数:7,代码来源:__init__.py

示例7: __init__

 def __init__(self,socket):
     Thread.__init__(self)
     (conn, (ip,port)) = socket.accept()
     self.conn = conn
     self.ip = ip
     self.port = port
     print "[+] New thread started for "+ ip+ ":"+port
开发者ID:swena56,项目名称:python-scripts,代码行数:7,代码来源:startNode.py

示例8: Serve

def Serve(socket, upload=True, download=True):
  while True:
    (s, addr) = socket.accept()
    if upload:
      thread.start_new_thread(Spew, (s,))
    if download:
      thread.start_new_thread(Sink, (s,))
开发者ID:7kbird,项目名称:chrome,代码行数:7,代码来源:netload.py

示例9: run

 def run(self):
     while not self._stop_event.is_set():
         events = self._poller.poll()
         for fd, flag in events:
             if fd == self._pipe[0]:
                 # Stop requested
                 for client in (client for client in self.fd_map.values() if client is not self.server):
                     self._poller.unregister(client)
                     client.close()
                 self.fd_map = {}
                 break
             socket = self.fd_map[fd]
             if flag & (select.POLLIN | select.POLLPRI):
                 # Ready to read
                 if socket is self.server:
                     sock, addr = socket.accept()
                     if self.use_ssl:
                         try:
                             client_sock = ssl.wrap_socket(sock,
                                                           server_side=True,
                                                           certfile=self.cert,
                                                           keyfile=self.key)
                         except ssl.SSLError:
                             sock.close()
                             continue
                     else:
                         client_sock = sock
                     client = self.client_cls(client_sock, self.handler)
                     self.fd_map[client.fileno()] = client
                     self._poller.register(client, READ_WRITE)
                 else:
                     data = socket.recv(1024)
                     if data:
                         socket.data_received(data)
                     else:
                         self.fd_map.pop(fd)
                         self._poller.unregister(socket)
                         socket.close()
             elif flag & select.POLLHUP:
                 # Client hung up
                 self.fd_map.pop(fd)
                 self._poller.unregister(socket)
                 socket.close()
             elif flag & select.POLLOUT:
                 # Ready to send
                 try:
                     data = socket.write_queue.get_nowait()
                 except Queue.Empty:
                     pass
                 else:
                     if data is WSClientClose:
                         socket.close()
                     else:
                         socket.send(data)
             elif flag & select.POLLERR:
                 # Error
                 self.fd_map.pop(fd)
                 self._poller.unregister(socket)
                 socket.close()
开发者ID:BioSoundSystems,项目名称:python-wsserver,代码行数:59,代码来源:wsserver.py

示例10: newConnections

def newConnections(socket):
    while True:
        sock, address = socket.accept()
        global total_connections
        connections.append(Client(sock, address, total_connections, "Name", True))
        connections[len(connections) - 1].start()
        print("New connection at ID " + str(connections[len(connections) - 1]))
        total_connections += 1
开发者ID:pricheal,项目名称:python-client-server,代码行数:8,代码来源:server.py

示例11: handle_request

 def handle_request(self, socket):
     request, client_address = socket.accept()
     request = ssl.wrap_socket(request, keyfile=server_pkey, certfile=server_cert,
                               server_side=True)
     self.log(client_address)
     prox = Proxy()
     prox.connect(request)
     self.proxy.append(prox)
开发者ID:keyvin,项目名称:docnotes,代码行数:8,代码来源:ssl_proxy.py

示例12: __prepare_read

 def __prepare_read(self, input_id):
     input_format, socket, conn, file_like = self.inputs[input_id]
     if not conn:
         socket.listen(1)
         c = socket.accept()
         new_conn, addr = c
         conn = new_conn
         self.inputs[input_id] = (input_format, socket, conn, conn.makefile())
开发者ID:pawellewandowski,项目名称:TIRT,代码行数:8,代码来源:sync.py

示例13: Accept

 def Accept( q, socket, port, id ):
   conn, addr = socket.accept()
   message = Message.fromRaw( conn.recv(1024) )
   if message.type == MessageType.AUTH:
     response = Message( MessageType.CONNECT, { "port" : port, "id" : id, "name" : message.get("name") } )
     q.put( response )
     conn.send( response.raw() )
     conn.close()
开发者ID:Alfwich,项目名称:school,代码行数:8,代码来源:Server.py

示例14: get_player_connections

def get_player_connections(socket, connections):
    while(len(connections) < MAX_PLAYERS):
        connection, client_addr = socket.accept()
        if connection and client_addr:
            connections.append([connection, client_addr])
            msg = connection.recv(MAX_MSG_SIZE)
            print("player connected with message:" + msg)
            connection.sendall("\"ok\"")
开发者ID:lydiaauch,项目名称:software-dev-2016,代码行数:8,代码来源:server.py

示例15: main

def main():
    print "Starting server..."
    options.read_configuration()
    socket = get_socket()
    while True:
        print "Waiting for clients on TCP port " + globals.port
        sock, addr = socket.accept()
        Connect(sock, addr).start()
开发者ID:DuVale,项目名称:wizards-magic,代码行数:8,代码来源:WizardsMagicServer.py


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