建立一個socketserver 至少分如下幾步:
- First, you must create a request handler class by subclassing the BaseRequestHandlerclass and overriding its handle() method; this method will process incoming requests.
- Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class.
- Then call the handle_request() orserve_forever() method of the server object to process one or many requests.
- Finally, call server_close() to close the socket.
一、本身建立一個請求處理類,
二、本身實例化一個TCP server ,而且傳遞server ip 和你上面建立的請求處理類
三、
import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The request handler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
while True:
try:
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
self.request.send(self.data.upper())
except ConnectionAbortedError as e:
print("err",e)
break
if __name__ == "__main__":
HOST, PORT = "localhost", 1111
# Create the server, binding to localhost on port 9999
server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
ThreadingTCPServer支持多線程
四、每一個用戶單獨字典
五、在linux上運行
七、mod5驗證
八、百分比
九、斷點續傳:暫停時將已傳文件大小(字節)存在臨時文件中,續傳時讀取臨時文件