Python学习--threading应用和socket应用

Python学习–threading应用和socket应用
代码示例:

#!/usr/bin/python

import threading
import random
from time import ctime, time, sleep
from socket import *

#TCP info
HOST = '10.1.252.95'
PORT = 6688
ADDR = (HOST, PORT)

BUFFSIZE = 1024

#thread numbers
THREAD_NUMS = 10

#send frequency
SEND_FREQ = 10000

#send info 
send_info_list = ['CidUserQuery {1001} {ME60-X16-ZJZ01-63201001000000@iptv} {}', 
                  'CidUserQuery4NAT {1001} {} {60.11.69.53} {}  {} ', 
                  'CidUserQuery {1002} {} {10.1.224.3}',
                  'CidUserQuery {1002} {} {10.1.224.4}',
                  'CidUserQuery {1002}  {10.1.224.5}',
                  'CidUserQuery {1002} {10.1.224.5}',
                  'CiUserQuery {1002} {} {10.1.224.5}']

#work thread class
class WorkThread(threading.Thread):
    def __init__(self, func, args, name):
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
        self.name = name
        self.res = None

    #get result
    def get_result(self):
        return self.res

    #run thread
    def run(self):
        print 'run loop start at:\n', ctime()
        self.res = apply(self.func, self.args)
        print 'run loop end at:\n', ctime()


#thread call-back function
def thread_func(thread_name):
    print '%s begin.\n' % thread_name
    #sleep(random.random()*1000)
    com_with_server(thread_name)
    print '%s end.\n' % thread_name

#init thread and run 
def run_thread():
    start_time = time()
    print __name__, ' Start at:\n', ctime()
    #thread pool
    threads = []
    #create work thread
    thread_nums = range(0, THREAD_NUMS)
    for i in thread_nums:
        thread_name = 'Thread[%d]' % i
        t = WorkThread(thread_func, (thread_name,), thread_func.__name__)
        threads.append(t)

    #run threads
    for i in thread_nums:
        threads[i].start()

    #wait thread end
    for i in thread_nums:
        threads[i].join()

    print __name__, ' End at:\n', ctime()
    end_time = time()
    print 'cost time:\n', (end_time - start_time)

#communicate with tcp server
def com_with_server(thread_name):
    for i in range(0, SEND_FREQ):
        try:
            #create client socket
            cli_sock = socket(AF_INET, SOCK_STREAM)
            #connect server
            cli_sock.connect(ADDR)
            #print '----2----\n'

            #sleep(random.random()*10)
            count = random.randint(0, len(send_info_list)-1)
            data = send_info_list[count]
            send_data = '%04d%s' % (len(data), data)
            print '%s send:[%s]\n' % (thread_name, send_data)

            #send info 
            cli_sock.send(send_data)

            #receive info
            recv_data = cli_sock.recv(BUFFSIZE)
            print '%s recv:[%s]\n' % (thread_name, recv_data)
            cli_sock.close()
        except Exception, e:
            print 'Error:%s\n' % e
            cli_sock.close()

#main 
if  __name__ == '__main__':
    print 'main begin\n'
    run_thread()
    print 'main end\n'

猜你喜欢

转载自blog.csdn.net/feng973/article/details/73896490