나는 간단한 파이썬 서버와 클라이언트를 가지고있다.클라이언트가 소켓을 닫을 때까지 소켓을 열어 두는 방법은 무엇입니까?
서버 :
import SocketServer
import threading
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(1024).strip()
print str(self.client_address[0]) + " wrote: "
print self.data
self.request.send(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 3288
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
클라이언트 :
여기import socket
import sys
from time import sleep
HOST, PORT = "localhost", 3288
data = "hello"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
sock.send(data + "\n")
received = sock.recv(1024)
sleep(10)
sock.send(data + "\n")
received = sock.recv(1024)
sleep(10)
sock.send(data + "\n")
received = sock.recv(1024)
finally:
sock.close()
입니다 출력 내가 얻을 :
서버 :
>python server.py
127.0.0.1 wrote:
hello
클라이언트 :
>python client.py
Traceback (most recent call last):
File "client.py", line 18, in <module>
received = sock.recv(1024)
socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
나는 리눅스 컴퓨터에서도이 도구를 사용해 보았습니다. 서버가 하나의 메시지 만 수신하면 두 번째 메시지의 recv 문에서 오류가 발생합니다. 나는 방금 파이썬에서 네트워킹을 배우기 시작했는데, 서버가 어떤 이유로 소켓을 닫고 있다고 생각한다. 이 문제를 어떻게 해결합니까?
도움이 될 수 있습니다. http://stackoverflow.com/a/20421867/2290820 – user2290820