初步实现服务器和终端能连接,并且终端向服务端发起请求并取得响应.,,,,

developer_wufayuan
wufayuan 3 years ago
parent 3aa4f82882
commit a24d80c675

@ -2,3 +2,4 @@
port = 7777
daemon = True

@ -0,0 +1,24 @@
2022-03-16 15:28:49.544 | INFO | __main__:<module>:8 - reading config args...
2022-03-16 15:28:49.544 | INFO | __main__:<module>:15 - starting the server...
2022-03-16 15:28:52.463 | INFO | dcs.tests.requestHandler:run:21 - [REQUEST] test
2022-03-16 15:28:52.464 | INFO | dcs.tests.requestHandler:run:31 - [RESPONSE] test: hello TEST, header size: 22
2022-03-16 15:28:52.464 | INFO | dcs.tests.server:run:37 - [REQUEST] end
2022-03-16 15:28:52.465 | WARNING | dcs.tests.server:run:38 - communication over!
2022-03-16 15:28:52.465 | WARNING | __main__:<module>:21 - Overing...
2022-03-16 15:29:18.818 | INFO | __main__:<module>:8 - reading config args...
2022-03-16 15:29:18.819 | INFO | __main__:<module>:15 - starting the server...
2022-03-16 15:29:21.915 | INFO | dcs.tests.requestHandler:run:21 - [REQUEST] test
2022-03-16 15:29:21.915 | INFO | dcs.tests.requestHandler:run:31 - [RESPONSE] test: hello TEST, header size: 22
2022-03-16 15:29:21.916 | INFO | dcs.tests.server:run:37 - [REQUEST] end
2022-03-16 15:29:21.916 | WARNING | dcs.tests.server:run:38 - communication over!
2022-03-16 15:29:21.916 | WARNING | __main__:<module>:21 - Overing...
2022-03-16 15:55:33.304 | INFO | __main__:<module>:8 - reading config args...
2022-03-16 15:55:33.304 | INFO | __main__:<module>:15 - starting the server...
2022-03-16 15:55:41.001 | INFO | dcs.tests.requestHandler:run:21 - [REQUEST] test
2022-03-16 15:55:41.002 | INFO | dcs.tests.requestHandler:run:31 - [RESPONSE] test: hello TEST, header size: 22
2022-03-16 15:56:16.305 | INFO | dcs.tests.requestHandler:run:33 - [REQUEST] translate
2022-03-16 15:56:16.305 | INFO | dcs.tests.spider:run:32 - crawling...
2022-03-16 15:56:17.145 | INFO | dcs.tests.requestHandler:run:44 - [RESPONSE] translate: Life is too short, time is like a boat, header size: 55
2022-03-16 15:56:17.146 | INFO | dcs.tests.server:run:36 - [REQUEST] end
2022-03-16 15:56:17.147 | WARNING | dcs.tests.server:run:37 - communication over!
2022-03-16 15:56:17.149 | WARNING | __main__:<module>:21 - Overing...

@ -4,18 +4,18 @@ from configparser import ConfigParser
from loguru import logger
logger.debug('reading config args...')
logger.add('./dcs.log', rotation='10 MB', enqueue=True, backtrace=True, diagnose=True)
logger.info('reading config args...')
configFile = '../conf/settings.ini'
con = ConfigParser()
con.read(configFile, encoding='utf-8')
items = con.items('server')
items = dict(items)
print(items)
logger.debug('starting the server...')
logger.info('starting the server...')
server = Server(int(items['port']))
server.daemon = items['daemon']
server.start()
server.join()
logger.debug('Overing...')
logger.warning('Overing...')

@ -68,6 +68,23 @@ class Client(Thread):
return responseJson['test']
def translate(self, word: str) -> 'int':
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
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 end(self):
"""
结束通信
@ -85,7 +102,8 @@ class Client(Thread):
print("end communication!")
def run(self) -> None:
self.test()
print(self.test())
print(self.translate(input("word:")))
self.end()

@ -2,6 +2,8 @@ import socket
import threading
import json
import struct
from loguru import logger
from dcs.tests.spider import Spider
class RequestHandler(threading.Thread):
@ -16,7 +18,7 @@ class RequestHandler(threading.Thread):
def run(self) -> None:
try:
if self.request_map['action'] == 'test':
print(f"[REQUEST] test")
logger.info(f"[REQUEST] test")
response = {
'test': 'hello TEST'
}
@ -26,9 +28,19 @@ class RequestHandler(threading.Thread):
response_binary = response_binary_len_binary + response_binary
self.client_socket.sendall(response_binary)
print(f"[RESPONSE] test: {response['test']}, header size: {response_binary_len}")
if self.request_map['action'] == 'crawler':
pass
# TODO
logger.info(f"[RESPONSE] test: {response['test']}, header size: {response_binary_len}")
elif self.request_map['action'] == 'translate':
logger.info(f"[REQUEST] translate")
spider = Spider(self.request_map['word'])
response = {
'translate': spider.run()
}
response_binary = json.JSONEncoder().encode(response).encode("utf-8")
response_binary_len = len(response_binary)
response_binary_len_binary = struct.pack("!Q", response_binary_len)
response_binary = response_binary_len_binary + response_binary
self.client_socket.sendall(response_binary)
logger.info(f"[RESPONSE] translate: {response['translate']}, header size: {response_binary_len}")
finally:
self.client_socket.close()

@ -3,6 +3,7 @@ import socket
import json
import struct
from dcs.tests.requestHandler import RequestHandler
from loguru import logger
def read_bytes(s: 'socket.socket', size: 'int') -> 'bytes':
@ -30,11 +31,10 @@ class Server(threading.Thread):
client_socket, _ = self.server_socket.accept()
request_header_size = struct.unpack("!Q", read_bytes(client_socket, 8))[0]
request_map = json.JSONDecoder().decode(read_bytes(client_socket, request_header_size).decode("utf-8"))
print(request_map)
# end请求要在主线程处理不然退出就不会及时响应
if request_map['action'] == 'end':
print(f"[REQUEST] end")
print("communication over!")
logger.info(f"[REQUEST] end")
logger.warning("communication over!")
break
r = RequestHandler(self, client_socket, request_map)
r.start()

@ -0,0 +1,34 @@
import threading
import requests
from loguru import logger
def crawl(word):
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {'i': word,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTIME',
'typoResult': 'false'}
r = requests.post(url, data)
answer = r.json()
result = answer['translateResult'][0][0]['tgt']
return result
class Spider(threading.Thread):
def __init__(self, word: 'string'):
super().__init__()
self.word = word
self.daemon = True
pass
def run(self) -> None:
logger.info('crawling...')
return crawl(self.word)

@ -1 +1,2 @@
吴法院
loguru~=0.6.0
requests~=2.27.1
Loading…
Cancel
Save