python socket 简单实现聊天功能
2014-08-26 21:53
986 查看
今天看了下python的socket,想实现下两台电脑的聊天功能(觉得这样好像自己写出个简易版QQ没问题了),于是动手开始实现。
首先是服务器端:from socket import *
from time import ctime
HOST=''
PORT=80 #端口号,服务器与客户端要一致
BUFSIZ=1024 #传送数据的缓冲区大小,随意设置
ADDR=(HOST,PORT) #链接地址
tcpSS=socket(AF_INET,SOCK_STREAM)
tcpSS.bind(ADDR)
tcpSS.listen(5)
while True:
print "waiting for connection.."
tcpCS,addr=tcpSS.accept()
print "...connected from:",addr
while True:
data=tcpCS.recv(BUFSIZ)
if not data:
exit(0)
print "[%s] \n%s"%(ctime() ,data)
data =raw_input('[%s]\n'%ctime())
tcpCS.send("%s"%data)
tcpCS.close()
tcpSS.close()接着是客户端:
from socket import *
from time import ctime
HOST='192.168.1.104'
PORT=80
BUFSIZ=1024
ADDR=(HOST,PORT)
tcpCS=socket(AF_INET,SOCK_STREAM)
tcpCS.connect(ADDR)
while True:
data=raw_input('[%s]\n'%ctime())
简单实现了客户端与服务器的一来一往的对话,并带有时间标签。只是为了玩一下。
首先是服务器端:from socket import *
from time import ctime
HOST=''
PORT=80 #端口号,服务器与客户端要一致
BUFSIZ=1024 #传送数据的缓冲区大小,随意设置
ADDR=(HOST,PORT) #链接地址
tcpSS=socket(AF_INET,SOCK_STREAM)
tcpSS.bind(ADDR)
tcpSS.listen(5)
while True:
print "waiting for connection.."
tcpCS,addr=tcpSS.accept()
print "...connected from:",addr
while True:
data=tcpCS.recv(BUFSIZ)
if not data:
exit(0)
print "[%s] \n%s"%(ctime() ,data)
data =raw_input('[%s]\n'%ctime())
tcpCS.send("%s"%data)
tcpCS.close()
tcpSS.close()接着是客户端:
from socket import *
from time import ctime
HOST='192.168.1.104'
PORT=80
BUFSIZ=1024
ADDR=(HOST,PORT)
tcpCS=socket(AF_INET,SOCK_STREAM)
tcpCS.connect(ADDR)
while True:
data=raw_input('[%s]\n'%ctime())
if not data: break tcpCS.send(data) data=tcpCS.recv(BUFSIZ) if not data: break print "[%s]\n%s"%(ctime(),data) tcpCS.close()主要就是数据接收和传送,没什么特别的地方。
简单实现了客户端与服务器的一来一往的对话,并带有时间标签。只是为了玩一下。
相关文章推荐
- 通过ClientSocket 与 ServerSocket实现简单的聊天功能.
- 使用Socket实现最简单的聊天功能
- socket 编程 TCP 实现简单聊天功能
- Python 3.x--Socket实现简单的ssh和文件下载功能
- Python基于Socket实现的简单聊天程序示例
- Python简单实现socket信息发送与监听功能示例
- Python基于socket实现简单的即时通讯功能示例
- python通过socket模拟实现简单的ssh功能
- Python+Socket实现基于TCP协议的客户与服务端中文自动回复聊天功能示例
- android Socket实现简单聊天功能以及文件传输
- Python Socket实现简单TCP Server/client功能示例
- Python socket 实现简单的server-client聊天
- Python socket实现的简单通信功能示例
- Android基于socket实现的简单C/S聊天通信功能
- socket 编程 TCP 实现简单聊天功能【转】
- python socket编程 实现简单p2p聊天程序
- iOS微信QQ聊天界面的UI框架以及Socket简单实现群聊功能
- Socket——实现简单的一对一服务器端与客户端聊天功能
- 直播项目,在线聊天功能--socket实现客户端和服务器端通讯的简单案例
- Socket UDP 简单聊天的实现