|
|
@ -52,7 +52,7 @@ class Client(Thread):
|
|
|
|
self.ip = ip
|
|
|
|
self.ip = ip
|
|
|
|
self.port = port
|
|
|
|
self.port = port
|
|
|
|
|
|
|
|
|
|
|
|
def test(self) -> 'int':
|
|
|
|
def test(self):
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
|
|
|
|
socket_to_server.connect((self.ip, self.port))
|
|
|
|
socket_to_server.connect((self.ip, self.port))
|
|
|
|
request = dict()
|
|
|
|
request = dict()
|
|
|
@ -68,7 +68,7 @@ class Client(Thread):
|
|
|
|
|
|
|
|
|
|
|
|
return responseJson['test']
|
|
|
|
return responseJson['test']
|
|
|
|
|
|
|
|
|
|
|
|
def translate(self, word: str) -> 'int':
|
|
|
|
def translate(self, word: str):
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
|
|
|
|
socket_to_server.connect((self.ip, self.port))
|
|
|
|
socket_to_server.connect((self.ip, self.port))
|
|
|
|
request = dict()
|
|
|
|
request = dict()
|
|
|
@ -85,7 +85,7 @@ class Client(Thread):
|
|
|
|
|
|
|
|
|
|
|
|
return responseJson['translate']
|
|
|
|
return responseJson['translate']
|
|
|
|
|
|
|
|
|
|
|
|
def crawling(self, word: str) -> 'int':
|
|
|
|
def crawling(self, word: str):
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
|
|
|
|
socket_to_server.connect((self.ip, self.port))
|
|
|
|
socket_to_server.connect((self.ip, self.port))
|
|
|
|
request = dict()
|
|
|
|
request = dict()
|
|
|
@ -102,6 +102,24 @@ class Client(Thread):
|
|
|
|
|
|
|
|
|
|
|
|
return responseJson['crawl zhiwang']
|
|
|
|
return responseJson['crawl zhiwang']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def report_status(self, status: str):
|
|
|
|
|
|
|
|
# status: free or busy
|
|
|
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
|
|
|
|
|
|
|
|
socket_to_server.connect((self.ip, self.port))
|
|
|
|
|
|
|
|
request = dict()
|
|
|
|
|
|
|
|
request['action'] = 'report_' + status
|
|
|
|
|
|
|
|
request['spider_info'] = (ip, port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
full_request = generate_request(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket_to_server.sendall(full_request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
responseJson = JSONDecoder().decode(
|
|
|
|
|
|
|
|
read_bytes(socket_to_server, struct.unpack('!Q', socket_to_server.recv(8))[0]).decode(
|
|
|
|
|
|
|
|
"utf-8"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return responseJson['report_'+status]
|
|
|
|
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
def end(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
结束通信
|
|
|
|
结束通信
|
|
|
@ -119,8 +137,9 @@ class Client(Thread):
|
|
|
|
print("end communication!")
|
|
|
|
print("end communication!")
|
|
|
|
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
def run(self) -> None:
|
|
|
|
print(self.test())
|
|
|
|
print(self.report_status('free'))
|
|
|
|
print(self.crawling(input("word:")))
|
|
|
|
print(self.crawling(input("word:")))
|
|
|
|
|
|
|
|
self.report_status('free')
|
|
|
|
self.end()
|
|
|
|
self.end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|