2015-11-18 57 views
2

這裏看到https://stackoverflow.com/search?q=TypeError+argument+must+be+an+int+or+have+a+fileno%28%29+method類型錯誤參數必須是int或擁有的fileno()方法

但就是無法找到挺我的答案。我正在嘗試一個聊天腳本

服務器端運行非常好。

# chat_server.py 

import sys 
import socket 
import select 

HOST = 'localhost' 
SOCKET_LIST = [] 
RECV_BUFFER = 4096 
PORT = 9009 

def chat_server(): 

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    server_socket.bind((HOST, PORT)) 
    server_socket.listen(10) 

    # add server socket object to the list of readable connections 
    SOCKET_LIST.append(server_socket) 

    print("Chat server started on port " + str(PORT)) 

    while 1: 

     # get the list sockets which are ready to be read through select 
     # 4th arg, time_out = 0 : poll and never block 
     ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0) 

     for sock in ready_to_read: 
      # a new connection request recieved 
      if sock == server_socket: 
       sockfd, addr = server_socket.accept() 
       SOCKET_LIST.append(sockfd) 
       print("Client (%s, %s) connected" % addr) 

       broadcast(server_socket, sockfd, "[%s:%s] entered our chatting room\n" % addr) 

      # a message from a client, not a new connection 
      else: 
       # process data recieved from client, 
       try: 
        # receiving data from the socket. 
        data = sock.recv(RECV_BUFFER) 
        if data: 
         # there is something in the socket 
         broadcast(server_socket, sock, "\r" + '[' + str(sock.getpeername()) + '] ' + data) 
        else: 
         # remove the socket that's broken  
         if sock in SOCKET_LIST: 
          SOCKET_LIST.remove(sock) 

         # at this stage, no data means probably the connection has been broken 
         broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr) 

       # exception 
       except: 
        broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr) 
        continue 

    server_socket.close() 

# broadcast chat messages to all connected clients 
def broadcast (server_socket, sock, message): 
    for socket in SOCKET_LIST: 
     # send the message only to peer 
     if socket != server_socket and socket != sock : 
      try : 
       socket.send(message) 
      except : 
       # broken socket connection 
       socket.close() 
       # broken socket, remove it 
       if socket in SOCKET_LIST: 
        SOCKET_LIST.remove(socket) 

if __name__ == "__main__": 

    sys.exit(chat_server()) 

但客戶

import sys 
import socket 
import select 

def chat_client(): 

    host = "127.0.0.1" 
    port = 9009 

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.settimeout(2) 

    # connect to remote host 
    try : 
     s.connect((host, port)) 
    except : 
     print('Unable to connect') 
     sys.exit() 

    print('Connected to remote host. You can start sending messages') 
    sys.stdout.write('[Me] '); sys.stdout.flush() 

    while 1: 
     socket_list = [sys.stdin, s] 
     potential_errs=[] 

     # Get the list sockets which are readable 
     ready_to_read,ready_to_write, in_error = select.select(socket_list , host, int(1)) 

     for sock in ready_to_read:    
      if sock == s: 
       # incoming message from remote server, s 
       data = sock.recv(4096) 
       if not data : 
        print('\nDisconnected from chat server') 
        sys.exit() 
       else : 
        #print data 
        sys.stdout.write(data) 
        sys.stdout.write('[Me] '); sys.stdout.flush()  

      else : 
       # user entered a message 
       msg = sys.stdin.readline() 
       s.send(msg) 
       sys.stdout.write('[Me] '); sys.stdout.flush() 

if __name__ == "__main__": 

    sys.exit(chat_client()) 

日前從CMD運行,不會因爲問題(讀一些有關它與我的問題)

回溯

line 50 in module 

    sys.exit(chat_client()) 

line 28 in chat_client 

    ready_to_read,ready_to_write, in_error = select.select(socket_list , host, int(1)) 

TypeError: argument must be an int or have a fileno() method 

任何幫助IDLE會很好。

+0

見這一個http://stackoverflow.com/questions/9737728/calling-select-in-python –

+0

'ready_to_read,ready_to_write,in_error = select.select(socket_list, [],[])',如果你想讓操作超時,添加另一個參數:'select.select(socket_list,[],[],1)' - 這會在一秒鐘後超時。 – CristiFati

+0

按照你的說法試過,有例外'OSError'。我假設這是因爲第二個參數不是套接字並嘗試連接到該套接字。因爲它提出了'贏得錯誤10038嘗試連接不是一個套接字.'試圖改變參數2主機,但然後'TypeError:參數必須是一個int或有一個fileno()方法'會回來 – Luke

回答

2

原來,select不適用於窗口上的非套接字對象。

(͡°͜ʖ͡°)

+0

我想知道,爲什麼?比爾叔叔又回來了嗎?看起來我現在必須使用線程。結果會看起來很醜。 – Luke

相關問題