类似字节的对象是必需的,而不是' str'

时间:2017-06-29 18:42:44

标签: python

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()

当我运行代码时,我收到此错误"类似字节的对象是必需的,而不是' str' " ,任何想法如何解决? 当我输入名字时,我收到此错误。

2 个答案:

答案 0 :(得分:4)

input()返回一个字符串,但send()需要字节。您需要对字符串进行编码:

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

答案 1 :(得分:0)

这里是代码(确保你已经安装了sockData模块):

model.null_deviance 
model.deviance