【发布时间】:2020-08-17 20:00:55
【问题描述】:
我正在尝试创建一个服务器,该服务器将接收来自客户端的消息并正确回答它们。 当我要求一个随机数(RAND)时,我收到此错误“需要一个类似字节的对象,而不是'int'”, 我该如何解决?
还有一个问题,我试图更改“recv”函数中的字节,但没有成功。有人可以帮帮我吗):?
import socket
import time
import random
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8820))
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
localtime = time.asctime( time.localtime(time.time()) )
ran = random.randint(0,10)
RUN = True
recieve = 1024
while RUN:
client_input = (client_socket.recv(recieve)).decode('utf8')
print(client_input)
if client_input == 'TIME':
client_socket.send(localtime.encode())
elif client_input == 'RECV':
recieve = client_socket.send(input("the current recieve amount is " + int(recieve) + ". Enter the recieve amount: "))
elif client_input == 'NAME':
client_socket.send(str("my name is SERVER").encode())
elif client_input == 'RAND':
client_socket.send(ran.encode())
elif client_input == 'EXIT':
RUN = False
else:
client_socket.send(str("I can only get 'TIME', 'NAME', 'RAND', 'EXIT'").encode())
client_socket.close()
server_socket.close()
【问题讨论】:
标签: python-3.x server atom-editor