A example code of multithread communication in Python
2008-12-26 09:55
806 查看
import socket
from threading import *
HOST=''
PORT=1777
class ServThread(Thread):
def run(self):
servSocket=socket.socket()
servSocket.bind((HOST,PORT))
servSocket.listen(5)
while 1:
conn,addr=servSocket.accept()
data=conn.recv(1024)
print 'Address is: %s /n' % repr(addr)
print 'Server eceived data is: %s /n' % data
conn.send('server send data')
conn.close()
break
servSocket.close()
class ClientThread(Thread):
def run(self):
clientSocket=socket.socket()
clientSocket.connect(('localhost',PORT))
clientSocket.send('client send data')
data=clientSocket.recv(1024)
print 'Client received data is: %s /n' % data
clientSocket.close()
def runtest():
servThread=ServThread()
servThread.start()
clientThread=ClientThread()
clientThread.start()
servThread.join()
clientThread.join()
if __name__=='__main__':
runtest()
from threading import *
HOST=''
PORT=1777
class ServThread(Thread):
def run(self):
servSocket=socket.socket()
servSocket.bind((HOST,PORT))
servSocket.listen(5)
while 1:
conn,addr=servSocket.accept()
data=conn.recv(1024)
print 'Address is: %s /n' % repr(addr)
print 'Server eceived data is: %s /n' % data
conn.send('server send data')
conn.close()
break
servSocket.close()
class ClientThread(Thread):
def run(self):
clientSocket=socket.socket()
clientSocket.connect(('localhost',PORT))
clientSocket.send('client send data')
data=clientSocket.recv(1024)
print 'Client received data is: %s /n' % data
clientSocket.close()
def runtest():
servThread=ServThread()
servThread.start()
clientThread=ClientThread()
clientThread.start()
servThread.join()
clientThread.join()
if __name__=='__main__':
runtest()
相关文章推荐
- Python example of SSH in multi-threads
- An Example of Perl Multi-thread 2
- Example Code of Using XML Parser in Symbian OS
- test run time of lines code in microsecond under Python3.6
- Face Recognition with Python, in Under 25 Lines of Code
- Code sample – socket client thread in Python
- A real example of vioplot in R (sample data and code attached)
- example of python operator overloadind
- Example code of searching in WordNet Dictionary with JAWS library
- An example code in .net of AWS EC2
- note of code in python
- A Basic Example of Threads Synchronization in Python, python中的线程同步示例
- I/O performance in Python - Python Performance 5 of n
- pip安装jupyter时报错Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-Fd4ir0/
- WinForms UI Thread Invokes: An In-Depth Review of Invoke/BeginInvoke/InvokeRequred
- Example of Unix commands implemented in Java
- The Story of self Parameter in Python, Demystified
- Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误的解决办法
- Dynamic load of a c++ dll in C#?http://www.codecomments.com/archive290-2004-7-216328.html
- return the index of a 'key' in Python dictionary