python 使用socket模拟tcp客户端和tcp服务器端
2016-09-09 16:08
627 查看
python 使用socket模拟tcp客户端和tcp服务器端
#coding=utf-8 #write by zxy987872674 ''' 服务器端代码 ''' import socket #创建套接字tcp tcpServerSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) address = ('',8080) tcpServerSocket.bind(address) tcpServerSocket.listen(5) while True: newServerSocket,destAddr = tcpServerSocket.accept() while True: recvData = newServerSocket.recv(1024) if len(recvData)>0: newServerSocket.send('thanks!') elif len(recvData) == 0: newServerSocket.close() print('----------') break tcpServerSocket.close() ''' 客户端代码 ''' import socket #创建套接字 tcpClientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print('socket---%s'%tcpClientSocket) #链接服务器 serverAddr = ('192.168.13.77',8080) tcpClientSocket.connect(serverAddr) print('connect success!') while True: #发送数据 sendData = raw_input('please input the send message:') if len(sendData)>0: tcpClientSocket.send(sendData) else: break #接收数据 recvData = tcpClientSocket.recv(1024) #打印接收到的数据 print('the receive message is:%s'%recvData) #关闭套接字 tcpClientSocket.close() print('close socket!')
相关文章推荐
- java-模拟tomcat服务器
- Linux socket 初步
- 小心服务器内存居高不下的元凶--WebAPI服务
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- 运维入门
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中