import json
|
import socket
|
|
import requests
|
|
import constant
|
from utils import socket_util
|
|
SOCKET_PORT = 11008
|
# B类
|
HTTP_PORT = 9004
|
|
|
# A类
|
# HTTP_PORT = 9005
|
|
|
def socket_request(data, host=constant.SERVER_HOST, port=SOCKET_PORT):
|
client = socket.socket() # 生成socket,连接server
|
ip_port = (host, port) # server地址和端口号(最好是10000以后)
|
client.connect(ip_port)
|
if type(data) != str:
|
client.send(socket_util.load_header(json.dumps(data).encode("utf-8")))
|
else:
|
client.send(socket_util.load_header(data.encode("utf-8")))
|
# 读取内容
|
result, header = socket_util.recv_data(client)
|
print("socket_request结果:", result)
|
client.close()
|
return result
|
|
|
def http_get(path):
|
response = requests.get(f"http://{constant.SERVER_HOST}:{HTTP_PORT}{path}")
|
if response.status_code == 200:
|
return response.text
|
return None
|