Python 发送消息后无法从服务器接收

Python could not receive from server after sending message over

我目前正在完成作业,我正在使用 python 套接字通过终端进行连接。但是,我遇到了一个问题,在我将消息发送到服务器并尝试接收其回复后,它有点挂起。我的代码如下:

import socket
import sys
import md5
import re

hostname = "cs2107.spro.ink"
ip = socket.gethostbyname(hostname)
port = 9000
server_address = (ip, port)
bufferSize = 1024

# Socket connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print >>sys.stderr, 'connecting to %s port %s' % server_address
s.connect((ip, port))

try: 
    while True:
        data = s.recv(bufferSize)
        if not data:
            break 

        print data

        regex = re.compile(r'\n([0-9a-fA-F]+)(?:\n)', re.I | re.S | re.M)
        checkHex = re.findall(regex, data)

        if len(checkHex) != 0:
            receiveHex = str(checkHex).strip("['']")
            decode = receiveHex.decode()

            m = md5.new()
            m.update(decode)
            hexReply = m.hexdigest()
            s.sendall(hexReply.encode())
            print hexReply


finally:
    print >>sys.stderr, 'closing socket.....'
    s.close()

输出显示在 link 中:output. After I kill it, it says the most recent call is `data = s.recv(bufferSize) link: after killing the terminal。任何人都知道如何解决这个问题?感谢您的帮助!

s.recv(bufferSize) 仍在等待来自服务器的一些传入数据。然而,服务器只发送您所看到的内容。尝试用您的 hexReply 变量回复服务器,看看会发生什么。只需在 print 语句后插入 s.send(hexReply + "\r\n")