2014-10-17 543 views
0

我几乎是Python Socket编程的初学者。我做了一个聊天服务器,但它不正确。聊天服务器引发TypeError:描述符'encode'需要'str'对象但收到'unicode'

它可以正常接收数据,但不适用于发送数据。当我使用'conn.send()'时,客户端永远不会收到消息。请帮帮我。

This is my code for the socket server: 

''' 
    Simple socket server using threads 
''' 

import socket 
import sys 
from _thread import * 

HOST = '' # Symbolic name meaning all available interfaces 
PORT = 8888 # Arbitrary non-privileged port 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print ('Socket created on Port: '+str(PORT)) 

#Bind socket to local host and port 
try: 
    s.bind((HOST, PORT)) 
except socket.error as msg: 
    print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) 
    sys.exit() 

print ('Socket bind complete') 

#Start listening on socket 
s.listen(10) 
print ('Socket now listening') 

connectmsg = 'Welcome to OmniBean\'s Chat server!' 
#Function for handling connections. This will be used to create threads 
def clientthread(conn): 
    #Sending message to connected client 
    print('Sending Welcome Message...') 
    #print(conn) 
    conn.send(str.encode(connectmsg)) #send only takes string ENCODED! 

    #infinite loop so that function do not terminate and thread do not end. 
    while True: 

     #Receiving from client 
     data = bytes.decode(conn.recv(1024)) 
     print (data) 
     reply = 'OK...' + data 
     if not data: 
      break 

     conn.sendall(str.encode(reply)) 

    #came out of loop 
    conn.close() 

#now keep talking with the client 
while 1: 
    #wait to accept a connection - blocking call 
    conn, addr = s.accept() 
    print ('Connected with ' + addr[0] + ':' + str(addr[1])) 
    conn.send(str.encode(connectmsg)) 
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function. 
    start_new_thread(clientthread ,(conn,)) 

s.close() 

如果你能弄明白这是为什么,那么你能告诉我吗?我的客户端代码是在这里: 我的客户是使用SimpleNet LibraryOmniBean

import os 
from simplenet import * 
myname = input ('Enter a login name: ') 
host = input('Enter Host Name: ') 
port = input('Enter Host Port: ') 
connect(host,port) 
welcome = receive() 
input('Received Message: '+welcome) 
while True: 
    os.system('cls') 
    #room = receive() 
    #print (room) 
    msg = input('Enter a message to send to server: ') 
    send(myname+': '+msg) 

从理论上讲,因为我从服务器发送数据的两倍,客户端应该接收数据;然而,客户端只是一直等待永远不会来自服务器的消息。请帮助我解决这个问题。

+0

'_thread'与'_thread import *'中的一样? – Paul 2014-10-17 05:48:11

+0

我用'thread'替换了'_thread',并用'telnet localhost 8888'作为客户端运行它。它给了我你好消息。在我输入一个字符串后,python程序给出了一个错误:'File'c​​hat.py',第42行,在clientthread中conn.sendall(str.encode(reply))TypeError:descriptor'encode'需要一个'str'对象,但收到'unicode' – Paul 2014-10-17 05:51:55

+0

该问题目前不是客户端/服务器问题。问题是'str.encode' – Paul 2014-10-17 05:55:46

回答

2

这不是客户/服务器问题。

我在测试你的脚本实际收到的错误是:

File "chat.py", line 42, in clientthread conn.sendall(str.encode(reply)) TypeError: descriptor 'encode' requires a 'str' object but received a 'unicode'

一般情况下,这将是后出现问题时的完整的错误信息有用....

谷歌的搜索位在上Python - Descriptor 'split' requires a 'str' object but received a 'unicode'

错误和下面的讨论中,我改变

conn.sendall(str.encode(reply))

conn.sendall(reply.encode('ascii'))

,现在它工作正常,我继续使用telnet localhost 8888作为客户端。

+1

非常感谢!我*永远感激*你保存了我的聊天服务器。但是,在我的机器上,没有错误! – OmniBean 2014-10-20 03:03:11