|
|
@ -4,6 +4,7 @@ from threading import Thread
|
|
|
|
import socket
|
|
|
|
import socket
|
|
|
|
from json import JSONEncoder, JSONDecoder
|
|
|
|
from json import JSONEncoder, JSONDecoder
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from dcs.tests.server import Server
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------配置--------------------------------------------
|
|
|
|
# -------------------------------配置--------------------------------------------
|
|
|
|
# ------------------------------config--------------------------------------------
|
|
|
|
# ------------------------------config--------------------------------------------
|
|
|
@ -43,14 +44,15 @@ def generate_request(request) -> 'bytes':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Client(Thread):
|
|
|
|
class Client(Thread):
|
|
|
|
def __init__(self, ip: str, port: int) -> None:
|
|
|
|
def __init__(self, server_ip: str, server_port: int) -> None:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
:param ip: 服务器IP
|
|
|
|
:param server_ip: 服务器IP
|
|
|
|
:param port: 服务器端口
|
|
|
|
:param server_port: 服务器端口
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
super().__init__()
|
|
|
|
super().__init__()
|
|
|
|
self.ip = ip
|
|
|
|
self.ip = server_ip
|
|
|
|
self.port = port
|
|
|
|
self.port = server_port
|
|
|
|
|
|
|
|
self.cookie = None
|
|
|
|
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
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:
|
|
|
@ -68,24 +70,6 @@ class Client(Thread):
|
|
|
|
|
|
|
|
|
|
|
|
return responseJson['test']
|
|
|
|
return responseJson['test']
|
|
|
|
|
|
|
|
|
|
|
|
def translate(self, word: str):
|
|
|
|
|
|
|
|
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'] = 'translate'
|
|
|
|
|
|
|
|
request['word'] = word
|
|
|
|
|
|
|
|
request['cookie'] = '2b0fd361bbf0b986fbc20d989a224d66fe9cb13a'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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['translate']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def crawling(self, word: str, pages_start: int, pages_end: int):
|
|
|
|
def crawling(self, word: str, pages_start: int, pages_end: int):
|
|
|
|
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))
|
|
|
@ -94,6 +78,7 @@ class Client(Thread):
|
|
|
|
request['word'] = word
|
|
|
|
request['word'] = word
|
|
|
|
request['pages_start'] = str(pages_start)
|
|
|
|
request['pages_start'] = str(pages_start)
|
|
|
|
request['pages_end'] = str(pages_end)
|
|
|
|
request['pages_end'] = str(pages_end)
|
|
|
|
|
|
|
|
request['cookie'] = '2b0fd361bbf0b986fbc20d989a224d66fe9cb13a'
|
|
|
|
|
|
|
|
|
|
|
|
full_request = generate_request(request)
|
|
|
|
full_request = generate_request(request)
|
|
|
|
|
|
|
|
|
|
|
@ -112,7 +97,7 @@ class Client(Thread):
|
|
|
|
request = dict()
|
|
|
|
request = dict()
|
|
|
|
request['action'] = 'report_' + status
|
|
|
|
request['action'] = 'report_' + status
|
|
|
|
request['spider_info'] = (ip, port)
|
|
|
|
request['spider_info'] = (ip, port)
|
|
|
|
request['cookie'] = '2b0fd361bbf0b986fbc20d989a224d66fe9cb13a'
|
|
|
|
request['cookie'] = self.cookie
|
|
|
|
|
|
|
|
|
|
|
|
full_request = generate_request(request)
|
|
|
|
full_request = generate_request(request)
|
|
|
|
|
|
|
|
|
|
|
@ -122,7 +107,7 @@ class Client(Thread):
|
|
|
|
read_bytes(socket_to_server, struct.unpack('!Q', socket_to_server.recv(8))[0]).decode(
|
|
|
|
read_bytes(socket_to_server, struct.unpack('!Q', socket_to_server.recv(8))[0]).decode(
|
|
|
|
"utf-8"))
|
|
|
|
"utf-8"))
|
|
|
|
|
|
|
|
|
|
|
|
return responseJson['report_'+status]
|
|
|
|
return responseJson['report_' + status]
|
|
|
|
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
def end(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -140,10 +125,30 @@ class Client(Thread):
|
|
|
|
|
|
|
|
|
|
|
|
print("end communication!")
|
|
|
|
print("end communication!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def login(self, user, password):
|
|
|
|
|
|
|
|
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'] = 'login'
|
|
|
|
|
|
|
|
request['user'] = user
|
|
|
|
|
|
|
|
request['password'] = password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if responseJson['login'] not in ['用户名错误,登录失败', '密码错误,登录失败']:
|
|
|
|
|
|
|
|
self.cookie = responseJson['login']
|
|
|
|
|
|
|
|
return responseJson['login']
|
|
|
|
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
def run(self) -> None:
|
|
|
|
|
|
|
|
print(self.login('2', '2'))
|
|
|
|
print(self.report_status('free'))
|
|
|
|
print(self.report_status('free'))
|
|
|
|
print(self.crawling(input("word:"), pages_start=3, pages_end=4)) # [3,4)
|
|
|
|
print(self.crawling(input("word:"), pages_start=1, pages_end=4)) # [3,4)
|
|
|
|
self.report_status('free')
|
|
|
|
|
|
|
|
self.end()
|
|
|
|
self.end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -151,3 +156,7 @@ download_task = Client(ip, port)
|
|
|
|
download_task.daemon = True
|
|
|
|
download_task.daemon = True
|
|
|
|
download_task.start()
|
|
|
|
download_task.start()
|
|
|
|
download_task.join()
|
|
|
|
download_task.join()
|
|
|
|
|
|
|
|
server = Server(7777)
|
|
|
|
|
|
|
|
server.daemon = True
|
|
|
|
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
server.join()
|
|
|
|