http_utils.py 1.31 KB
Newer Older
oscar's avatar
oscar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
# -*- coding:utf-8 -*-
import hashlib
import json
import traceback
import logging
import threading
import requests


def upload_thread(msgs, ipc_code, url):
    threading.Thread(target=upload_data, args=[msg, ipc_code, url]).start()

def upload_data(msgs, ipc_code, url):
    request_data = {
        "devNo": ipc_code,
        "mecNo": ipc_code,
        "objs": []
    }
    for msg in msgs:
        request_data["objs"].append({
	    "heading": msg.rot_y,
	    "id": msg.obj_id,
	    "lat": msg.Lat,
	    "lon": msg.Long,
	    "name": msg.name,
	    "speed": node["speed"],
	    "time": get_current_millisecond(),
	    "type": node["obj_type"]
        })
    request_json_pure(url, request_data, http_timeout=1)


def request_json_pure(http_url, data, http_timeout=10):
    """
    http json 协议传输 无校验

    :param http_url:
    :param data:
    :param http_timeout:
    :return:
    """
    header_data = {
        "Content-Type": "application/json",
        'Connection': 'keep-alive'
    }
    data_json = json.dumps(data, ensure_ascii=False, separators=(',', ':'))
    print(data_json)
    s = requests.session()
    response = s.post(http_url, data=data_json, headers=header_data, timeout=http_timeout)
    result = response.text
    print(result)
    response.close()
    result = json.loads(result)
    return result