获得不需要的输出(新手)

时间:2017-03-17 05:05:58

标签: python sockets

我正在尝试使用python学习套接字编程,但不断得到不需要的输出,即我期待" BIG"而不是" b' BIG'"。 我哪里出错了? 提前谢谢。

-w.e。

在客户端(tcpclient.py): 输出: -

C:>python tcpclient.py
Lowercase:big
Reply from Server:  b'BIG'
C:>

我的编码如下:

在客户端(tcpclient.py):

    from socket import *
    serverName = '127.0.0.1'
    serverPort = 12000
    clientSocket = socket(AF_INET, SOCK_STREAM)
    clientSocket.connect((serverName,serverPort))
    sentence = input("Lowercase sentence:")
    clientSocket.send(sentence.encode())
    modifiedSentence = clientSocket.recv(1024)
    print ("Reply from Server: ",modifiedSentence)
    clientSocket.close()

在服务器(tcpserver.py)

    from socket import *
    serverPort = 12000
    serverSocket = socket(AF_INET,SOCK_STREAM)
    serverSocket.bind(('',serverPort))
    serverSocket.listen(1)
    print("The server is ready to receive")
    while 1:
        connectionSocket, addr = serverSocket.accept()
        sentence = connectionSocket.recv(1024).decode()
        capitalizedSentence = sentence.upper()
        connectionSocket.send(capitalizedSentence.encode())
        connectionSocket.close()

0 个答案:

没有答案