服务器不响应来自一个客户端

时间:2017-04-11 14:34:57

标签: python sockets

我尝试实现一次只响应一个客户端和客户端的服务器。对于客户端,我只使用了一个连接,可以向服务器发送多个请求。对于第一个请求,一切顺利。对于第一个请求之后的所有请求,客户端将报告错误:socket.error: [Errno 10053] An established connection was aborted by the software in your host machine 。我发布了我的服务器和客户端代码以及下面的示例测试:

服务器部分:

import socket
import re
#create server socket. AF_INET-> ipv4. SOCK_STREAM->socket type
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 20000
#bind the socket to localhost and a well-know port
serverSocket.bind((socket.gethostname(), port))
serverSocket.listen(3)

while True:
    clientSocket, addr = serverSocket.accept()
    print("Got a connection from %s" % str(addr))
    request = clientSocket.recv(1024).decode()
    print('Server received', repr(request))
    splitRequest = re.split('\<|\>', request)
    if splitRequest[0] == 'EXIT':
        if len(splitRequest) == 1:
            print "Normal exit"
        elif len(splitRequest) > 1:
            print splitRequest[1]
    else:
        if splitRequest[0] == 'GET':
            fileName = splitRequest[1]
            path = 'test_files/' + fileName
            try :
                with open(path, 'r') as serverFile:
                    message = serverFile.read()
                    print message
                    clientSocket.send(message)
            except Exception as e:
                message = str(e)
                print message
                clientSocket.send(message)
        elif splitRequest[0] == 'BOUNCE':
            message = splitRequest[1]
            print message
            clientSocket.sendall(message)
    clientSocket.close()

客户方:

import socket
import re
def isValidRequest(input):
    if re.match('GET\<.+\>', input) or re.match('BOUNCE\<.+\>', input) or input == 'EXIT' or re.match('EXIT\<.+\>', input):
        return True
    return False
def receiveAll(socket):
    BUFF_SIZE = 4096  # 4 KiB
    data = ""
    while True:
        part = socket.recv(BUFF_SIZE)
        data += part
        if len(part) < BUFF_SIZE:
            # either 0 or end of data
            break
    return data


# create a client socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
# port number
port = 20000
# connection to hostname on the port.
s.connect((host, port))
while True:
    request = raw_input()
    if isValidRequest(request):
        if request == 'EXIT' or re.match('EXIT\<.+\>', request):
            s.send(request)
            break
        else:
            s.send(request)
            print "after send"
            content = receiveAll(s)

            print content
    else:
        print "Invalid request, please enter it again!"
# close client connection
s.close()

我从服务器运行两次请求相同的txt文件 - “MACD.txt”的测试。控制台中的输入是“GET”。客户端控制台中的打印消息:

*GET<MACD.txt>*
after send
MACD, short for moving average convergence/divergence, is a trading indicator used in technical analysis of stock prices, created by Gerald Appel in the late 1970s.[1] It is supposed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock's price.
*GET<MACD.txt>*
socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
after send

服务器部分中的打印消息。并且您可以看到第一个请求的仅服务器打印消息:

Got a connection from ('192.168.126.1', 60567)
('Server received', "u'GET<MACD.txt>'")
MACD, short for moving average convergence/divergence, is a trading indicator used in technical analysis of stock prices, created by Gerald Appel in the late 1970s.[1] It is supposed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock's price.

我很困惑,因为我在Stackoverflow中搜索相同的问题并且没有一个与我的情况相符。我还阅读了python套接字的文档但仍然没有任何内容

0 个答案:

没有答案