pythonカスタマーサービス側サービス側マルチスレッド通信

4891 ワード

1.サービス側
メインプログラム:
# encoding: utf-8
import threading

from procedure.socket_server import ThreadingHttpServer, MainHandler
from procedure.process import mq_respond_procedure
from logger import log,logwf
from conf import (HOST,
                  PORT,
                  MQ_A_RECV_1,
                  MQ_A_RECV_2,
                  MQ_A_RECV_3,
                  MQ_A_RECV_4,
                  MQ_B_RECV_1,
                  MQ_B_RECV_2,
                  MQ_B_RECV_3,
                  MQ_B_RECV_4)


def start_server():
    log.info("          ")
    print("          ")

    threads = []
    server = ThreadingHttpServer((HOST, PORT), MainHandler)
    server_thread = threading.Thread(target=server.serve_forever)
    threads.append(server_thread)

    mq_a1_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_A_RECV_1, ))
    mq_a2_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_A_RECV_2, ))
    mq_a3_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_A_RECV_3, ))
    mq_a4_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_A_RECV_4, ))
    mq_b1_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_B_RECV_1, ))
    mq_b2_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_B_RECV_2, ))
    mq_b3_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_B_RECV_3, ))
    mq_b4_thread = threading.Thread(target=mq_respond_procedure, args=(MQ_B_RECV_4, ))
    threads.extend([mq_a1_thread, mq_a2_thread, mq_a3_thread, mq_a4_thread, mq_b1_thread, mq_b2_thread, mq_b3_thread, mq_b4_thread])

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()


if __name__ == "__main__":
    start_server()

ソケットサービス:
 encoding: utf-8
from http.server import BaseHTTPRequestHandler
from socketserver import ThreadingMixIn, TCPServer
import threading

from logger import log
from conf import HOST, PORT
from procedure.process import send_total_procedure


log_id = 10000000
id_lock = threading.Lock()


class ThreadingHttpServer(ThreadingMixIn, TCPServer):
    pass


class MainHandler(BaseHTTPRequestHandler):
    # http        

    def do_POST(self):
        '''  post  '''

        content_len = int(self.headers['content-length'])  # 

2.クライアント
import os
import time
import http.client
import re


def read_config():
    '''
          ip     
    :return: ip  ,   
    '''
    with open("./config.txt", "r", encoding="utf-8") as f:
        data = f.readlines()
    host_ip = ""
    port_num = 0
    send_file_list = []

    msg_line = data.index("---      ---
") for index in range(len(data)): if re.search(r"HOST_IP \=(.+)", data[index]): host_ip = re.search(r"HOST_IP \=(.+)", data[index]).group(1).strip() elif re.search(r"PORT \=(.+)", data[index]): port_num = int(re.search(r"PORT \=(.+)", data[index]).group(1).strip()) if index > msg_line: if data[index]: send_file_list.append(data[index]) return host_ip, port_num, send_file_list def read_file(filename): ''' message , :filename: :return: ''' file_list = [] for file in os.listdir('./message'): file_list.append(file) if filename in file_list: with open(os.path.join('./message', filename), 'rb') as f: return {filename: f.read()} # { : } else: return None def send_process(host_ip, port_num, mq_addr_list, filename): ''' :param host_ip: ip :param port_num: :param mq_addr_list: mq ip :return: , 。 , 。 ''' msg = read_file(filename) if not msg: print(" :", filename) return headers = {"mq-addr": str(mq_addr_list)} print(" ") conn = http.client.HTTPConnection(host_ip, port_num) print(" ") conn.request(url=host_ip, method="POST", headers=headers, body=str(msg)) response = conn.getresponse().read().decode("utf-8") print(response) return if __name__ == "__main__": host_ip, port_num, send_file_list = read_config() for item in send_file_list: mq_addr_list = [] if item.strip(): time.sleep(int(item.split(',')[2])) for addr in item.split(',')[1].split('/'): mq_addr_list.append(addr) send_process(host_ip, port_num, mq_addr_list, item.split(',')[0])