I am currently working on my assignment and I am using python socket to connect through terminal. However, I encountered a problem where after I send my message to the server and try to receive its reply, it kind of hangs. My codes are as follows:
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()
The output is shown in the link: output. After I kill it, it says the most recent call is `data = s.recv(bufferSize) link: after killing the terminal. Anyone have any idea how to solve this? Appreciate your help!