2017-06-29 74 views
0
import socket 

IP = "127.0.0.1" 
PORT = 5200 

# Create a TCP/IP socket 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

# Connecting to remote computer 80 
server_address = (IP,PORT) 
sock.connect(server_address) 
# Sending data to server 


x = input('enter firstname:') 
sock.send(x) 
y = input("enter lastname:") 
sock.send(y) 

server_msg = sock.recv(1024) 
print (server_msg) 


# Closing the socket 
sock.close() 

當我運行的代碼它,我得到這個錯誤「一類字節對象是必需的,而不是‘海峽’」,任何想法如何解決呢? 當我輸入名字時,出現此錯誤。類字節對象是必需的,而不是「海峽」

回答

3

input()返回一個字符串,但send()需要字節。您需要對字符串進行編碼:

x = input('enter firstname:') 
sock.send(x.encode("utf-8")) 
y = input("enter lastname:") 
sock.send(y.encode("utf-8")) 
相關問題