我开始学习网络,并在其中一项练习中被要求 写一个服务器,对同一客户端多次响应 而我得到了这个错误
socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
我想问问题是否出在软件而不是我的程序上,或者是我的程序上。 这是我的服务器和客户端(我警告他们很长:D)
服务器:
import socket
import time
import random
def message_init(client_socket):
"""
this method gets the socket of the client
and waiting for a command from the client
if the command is one of the commands the
server is familiar with, the method returns
a proper message. if its not a known command
to the server, the server sends to the client
'null' in order that it'll enter a new valid
command.
"""
# starting message with 'Garbage' in case when
# the client will insert unsupported commands
message = 'Garbage'
while message == 'Garbage':
# waiting for a command from the client
client_command = client_socket.recv(1024)
length = client_command[0: 2:]
request = client_command[2::]
# message is the proper reply for the request from the client
if request == 'TIME':
# message is the local time
message = time.asctime(time.localtime(time.time()))
elif request == 'NAME':
# message is the name of the server
message = "Daniel's Server"
elif request == 'RAND':
# message is a random integer between 1 - 10
message = str(random.randint(1, 10))
elif request == 'EXIT':
# message is exit and closing the connection with the client
message = 'EXIT'
client_socket.send(message)
client_socket.close()
if request == 'Garbage':
# sending 'null' to the client
# in order that he'll sent a valid command
client_socket.send('null')
# returns a message in proper to the command
return message
def send_message(client_socket, message):
"""
Gets a message and the socket of the client
sending to the client the number of bytes
in the message and afterwards sending the
message itself
"""
new_message = ''
length = len(message)
if length < 10:
new_message += '0'
new_message += str(length) + message
# sending the message to the client
client_socket.send(new_message)
def main():
"""
binding with every client that tries to connect.
getting a command from him, and if the command is
valid, the server does what the command asks for.
The proper Commands:
TIME: returns a string of the local time.
NAME: returns a string of the name of the server.
RAND: returns a string of number between 1 - 10.
EXIT: closing the connection with the client
"""
# creates a new socket
server_socket = socket.socket()
try:
# binding with every client who asks for
server_socket.bind(('0.0.0.0', 1729))
# waiting to get connection from a client
server_socket.listen(1)
except socket.error, e:
print e
connected = False
# an infinite loop of connecting with a client
# getting a command from a client and sending
# him proper response
while True:
if not connected:
# getting the client socket and his address
# and accepting the new connection
(client_socket, client_address) = server_socket.accept()
connected = True
# message gets a valid message using message_init function
message = message_init(client_socket)
if message != 'EXIT':
# if there's still connection:
send_message(client_socket, message)
else:
connected = False
client_socket.close()
# closing the socket of the server
# (although on the current server and client it won't happened)
server_socket.close()
if __name__ == '__main__':
main()`
客户:
import socket
def valid_cmd(command, commands):
"""
getting a command and a list of
approved commands. returning the
command when its one of the commands
"""
while command not in commands:
# waiting until the client enters a valid command
command = raw_input("Please enter one command of the following:"
" TIME, NAME, RAND or EXIT ")
# returns the command if its in commands
return command
def send_request_to_server(my_socket, request):
"""
getting a request and the socket
sending to the server the request
when there is its length before him
in two digits.
for example: 04RAND
04EXIT
"""
length = len(request)
message = ''
# putting a zero before so the length will
# be in two digits form
if length < 10:
message += '0'
message += str(length) + request
# sending the message to the server
my_socket.send(message)
def handle_server_response(my_socket, commands):
"""
Receive the response from the server
and handle it, according to the request
"""
# data gets the response from the server
data = my_socket.recv(1024)
# if the server returned null there was
# a problem with the message so we ask
# from the client to give us other message
while data == 'null':
message = raw_input("Please enter one command of the following:"
"TIME, NAME, RAND or EXIT")
# checks whether the message is valid
message = valid_cmd(message, commands)
# sending the message to the server
send_request_to_server(my_socket, message)
# waiting for the server's response
data = my_socket.recv(1024)
# returns the data when it has valid value
return data
def main():
"""
connecting to the home server
sending it a command
and printing the server's answer
"""
# creating a new socket
my_socket = socket.socket()
try:
# connecting the client to the home server
my_socket.connect(('127.0.0.1', 1729))
except Exception, e:
print e
my_socket.close()
# a list of the approved commands
commands = ['TIME', 'NAME', 'RAND', 'EXIT']
stopped = False
while not stopped:
# asking for an input of command
message = raw_input()
try:
# making sure that message is one of the valid commands
message = valid_cmd(message, commands)
# sending the message to the server
send_request_to_server(my_socket, message)
except Exception, e:
my_socket.close()
print e
# data gets a valid response from the server
data = handle_server_response(my_socket, commands)
# checking it the server disconnected from the client
if data == 'EXIT':
stopped = True
# keeping the length of the upcoming data
# although there is no use for that on that program
length = data[0: 2]
# printing the data from the server
print data[2::]
# disconnecting from the server
my_socket.close()
lst = ['a', 'b', 'c']
assert valid_cmd('b', lst) == 'b', 'valid_cmd should return' \
' the given value if its ' \
'already in the given list'
if __name__ == '__main__':
main()