server_communication.py 1.1 KB
'''
@ author: jie
@ tools: pycharm
@ content: 服务器通讯实现类
@ date: 2021.6.3
'''

import logging
from config import Config
import requests


class ServerCommunication():
    def __init__(self):
        self.ip_path = Config.IP_PATH
        self.ip_config = {}
        try:
            with open(self.ip_path + 'ipconfig.csv', 'r') as f:
                reader = csv.reader(f)
                current = [row for row in reader]
                self.ip_config['ip'] = current[0][0]
                self.ip_config['cid'] = current[0][1]
                logging.warning("ip&cid...:{}".format(self.ip_config))
        except Exception as e:
            logging.warning("ip,cid配置失败:{}".format(e))

    def server_post(self,body):
        ip_config = self.ip_config
        url = ip_config['ip']
        cid = ip_config['cid']
        headers = {'content-type': "application/json"}
        try:
            response = requests.post(url, data = json.dumps(data), headers = headers,timeout=2)
            json_data = response.json()
        except Exception as e:
            logging.warning("服务器通信失败:{}".format(e))