python执行多线程的实例
2012-03-20 11:26
417 查看
#-*- encoding: gb2312 -*-
import threading
import time
from socket import *
class MyThread(threading.Thread):
def __init__(self,host,port,msg,BF):
threading.Thread.__init__(self)
self.host = host
self.port = port
self.msg = msg
self.BF = BF
def run(self):
print 'connect server and get the DevIpAddress '+str(self.msg)
port = int(self.port)
address=(self.host,port)
print address
clientSocket=socket(AF_INET,SOCK_STREAM)
#创建采用TCP 协议的socket对象
clientSocket.connect(address)
#连接指定的服务器端
while True:
time.sleep(3)
clientSocket.send(self.msg)
msg=clientSocket.recv(self.BF)
if (len(msg) > 0):
print msg
def test():
thread1 = MyThread(‘127.0.0.1’,7071,'aaa',1024)
thread2 = MyThread(‘127.0.0.1’,7071,'ccc',1025)
thread1.start()
thread2.start()
time.sleep(10)
if __name__ == '__main__':
test()
import threading
import time
from socket import *
class MyThread(threading.Thread):
def __init__(self,host,port,msg,BF):
threading.Thread.__init__(self)
self.host = host
self.port = port
self.msg = msg
self.BF = BF
def run(self):
print 'connect server and get the DevIpAddress '+str(self.msg)
port = int(self.port)
address=(self.host,port)
print address
clientSocket=socket(AF_INET,SOCK_STREAM)
#创建采用TCP 协议的socket对象
clientSocket.connect(address)
#连接指定的服务器端
while True:
time.sleep(3)
clientSocket.send(self.msg)
msg=clientSocket.recv(self.BF)
if (len(msg) > 0):
print msg
def test():
thread1 = MyThread(‘127.0.0.1’,7071,'aaa',1024)
thread2 = MyThread(‘127.0.0.1’,7071,'ccc',1025)
thread1.start()
thread2.start()
time.sleep(10)
if __name__ == '__main__':
test()
相关文章推荐
- python执行多线程的实例
- Python多线程经典问题之乘客做公交车算法实例
- 初识python多线程(转+实例)
- Python多线程编程实例【python教程入门】
- python多线程方式执行多个bat代码
- python 8-1 如何使用多线程,Thread创建线程,执行函数赋值给target//类+函数放在run方法中执行
- python多线程操作实例
- Python简单多线程实例
- 关于多线程执行显示进度条的实例!
- 在PYTHON中执行SHELL的实例
- Python 爬虫多线程详解及实例代码
- python paramiko实现多线程远程执行命令、多线程远程上传文件、多线程远程下载文件
- 《关于多线程执行显示进度条的实例!》--更接近于实战的实例
- 利用python多线程执行远程linux上命令
- python中的多线程实例教程
- Python多线程下载文件实例代码
- python多线程用法实例详解
- python多线程代码实例