这是老师给我们的代码,用于我们的作业中。应该不会有错误。但是还有。请帮我修复它。 (错误在第9行)
import socket
serverPort = 12000
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print ("The server is ready to receive")
while 1:
connectionSocket, addr = serverSocket.accept()
byte_sentence = connectionSocket.recv(1024)
utf_sentence = byte_sentence.decode("utf-8")
modified_utf_sentence = utf_sentence.upper()
modified_byte_sentence = bytes(modified_utf_sentence, "utf-8")
connectionSocket.send(modified_byte_sentence)
connectionSocket.close()
文件“ tcpserver.py”,第9行 connectionSocket,addr = serverSocket.accept() ^ IndentationError:预期出现缩进的块
答案 0 :(得分:0)
在声明while循环后,您需要将代码缩进4个空格。
import socket
serverPort = 12000
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print ("The server is ready to receive")
while 1:
connectionSocket, addr = serverSocket.accept()
byte_sentence = connectionSocket.recv(1024)
utf_sentence = byte_sentence.decode("utf-8")
modified_utf_sentence = utf_sentence.upper()
modified_byte_sentence = bytes(modified_utf_sentence, "utf-8")
connectionSocket.send(modified_byte_sentence)
connectionSocket.close()
while循环中的所有内容都必须缩进4个空格。返回未缩进的代码将终止while循环。