Python聊天室程序(基础版)


Posted in Python onApril 01, 2018

本文实例为大家分享了Python聊天室程序的具体代码,供大家参考,具体内容如下

客户端代码:

# Filename: socketClient.py 
 
import socket 
import sys 
import threading 
 
# Client GUI 
from tkinter import * 
import Pmw 
 
 
 
# Create a TCP/IP socket 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
# Connect the socket to the port where the server is listening 
server_address = ('localhost', 10000) 
print (sys.stderr, 'connecting to %s port %s' % server_address) 
sock.connect(server_address) 
 
root = Tk() 
# textDisplay 
textDisplay = Pmw.ScrolledText(root) 
textDisplay.pack(expand=1, padx=5, pady=5,side = LEFT) 
# textInput 
textInput = Pmw.ScrolledText(root) 
textInput.pack(expand=1, padx=5, pady=5,side = LEFT) 
# Send Button and its callback 
def sendMsg(event): 
 message = socket.gethostname()+':'+ textInput.get() 
 #print (sys.stderr, 'sending "%s"' % message) 
 print(message) 
 sock.sendall(message.encode()) 
 textInput.clear() 
 #data = sock.recv(100) 
 #textDisplay.insert(END, data) 
 #print (sys.stderr, 'received "%s"' % data) 
  
sendBtn = Button(root, text="Send") 
sendBtn.bind('<Button-1>', sendMsg) 
sendBtn.pack(side = LEFT) 
 
def receiveMsg(): 
 while True: 
  data = sock.recv(100) 
  print (sys.stderr, 'client received "%s"' % data) 
  textDisplay.insert(END, data) 
  
 
receiveThread = threading.Thread(name='waitForMSG', target=receiveMsg) 
receiveThread.start() 
 
root.mainloop()

 服务器端代码:

# Filename: socketServer.py 
 
import socket 
import sys 
 
# Create a TCP/IP socket 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
 
# Bind the socket to the port 
server_address = ('localhost', 10000) 
print (sys.stderr, 'starting up on %s port %s' % server_address) 
sock.bind(server_address) 
 
# Listen for incoming connections 
sock.listen(1) 
 
while True: 
 # Wait for a connection 
 print (sys.stderr, 'waiting for a connection') 
 connection, client_address = sock.accept() 
 
 try: 
  print (sys.stderr, 'connection from', client_address) 
 
  # Receive the data in small chunks and retransmit it 
  while True: 
   data = connection.recv(16) 
   print (sys.stderr, 'received "%s"' % data) 
   if data: 
    print (sys.stderr, 'sending data back to the client') 
    connection.sendall(data) 
   else: 
    print (sys.stderr, 'no data from', client_address) 
    break 
 finally: 
  # Clean up the connection 
  connection.close()

客户端在监听服务器的消息采用了多线程的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python采用requests库模拟登录和抓取数据的简单示例
Jul 05 Python
Python处理json字符串转化为字典的简单实现
Jul 07 Python
分享Python开发中要注意的十个小贴士
Aug 30 Python
使用python实现tcp自动重连
Jul 02 Python
Django 使用logging打印日志的实例
Apr 28 Python
Python使用matplotlib实现基础绘图功能示例
Jul 03 Python
用Python编写一个简单的CS架构后门的方法
Nov 20 Python
使用Python的SymPy库解决数学运算问题的方法
Mar 27 Python
Pythony运维入门之Socket网络编程详解
Apr 15 Python
python使用celery实现异步任务执行的例子
Aug 28 Python
python 命令行传参方法总结
May 25 Python
深入浅析Django MTV模式
Sep 04 Python
Python socket实现简单聊天室
Apr 01 #Python
简单实现python聊天程序
Apr 01 #Python
简单实现Python爬取网络图片
Apr 01 #Python
Python中elasticsearch插入和更新数据的实现方法
Apr 01 #Python
python之DataFrame实现excel合并单元格
Feb 22 #Python
python合并同类型excel表格的方法
Apr 01 #Python
python实现两个文件合并功能
Apr 01 #Python
You might like
PHP5常用函数列表(分享)
2013/06/07 PHP
PHP+Javascript实现在线拍照功能实例
2015/07/18 PHP
Javascript技巧之不要用for in语句对数组进行遍历
2010/10/20 Javascript
jQuery Ajax请求状态管理器打包
2012/05/03 Javascript
Extjs 3.3切换tab隐藏相应工具栏出现空白解决
2013/04/02 Javascript
NodeJS的url截取模块url-extract的使用实例
2013/11/18 NodeJs
jquery 通过name快速取值示例
2014/01/24 Javascript
教你如何自定义百度分享插件以及bshare分享插件的分享按钮
2014/06/20 Javascript
零基础搭建Node.js、Express、Ejs、Mongodb服务器及应用开发入门
2014/12/20 Javascript
js实现发送验证码后的倒计时功能
2015/05/28 Javascript
js倒计时小实例(多次定时)
2016/12/08 Javascript
javascript中call,apply,bind函数用法示例
2016/12/19 Javascript
JS中Attr的用法详解
2017/10/09 Javascript
vue mint-ui 实现省市区街道4级联动示例(仿淘宝京东收货地址4级联动)
2017/10/16 Javascript
JS数组扁平化(flat)方法总结详解
2019/06/24 Javascript
vue 实现setInterval 创建和销毁实例
2020/07/21 Javascript
解决父组件将子组件作为弹窗调用只执行一次created的问题
2020/07/24 Javascript
Python实现的一个找零钱的小程序代码分享
2014/08/25 Python
python编写朴素贝叶斯用于文本分类
2017/12/21 Python
Linux下Pycharm、Anaconda环境配置及使用踩坑
2018/12/19 Python
利用Python查看微信共同好友功能的实现代码
2019/04/24 Python
python实现两个dict合并与计算操作示例
2019/07/01 Python
python使用 zip 同时迭代多个序列示例
2019/07/06 Python
python中使用while循环的实例
2019/08/05 Python
pytorch 模型可视化的例子
2019/08/17 Python
python实现word文档批量转成自定义格式的excel文档的思路及实例代码
2020/02/21 Python
Django DRF APIView源码运行流程详解
2020/08/17 Python
python3 通过 pybind11 使用Eigen加速代码的步骤详解
2020/12/07 Python
英国复古皮包品牌:Beara Beara
2018/07/18 全球购物
快时尚眼镜品牌,全国连锁眼镜店:LOHO眼镜生活
2018/10/08 全球购物
个人党性剖析材料
2014/02/03 职场文书
电子信息工程专业推荐信
2014/02/14 职场文书
自查自纠整改报告
2014/11/06 职场文书
施工安全员岗位职责
2015/04/11 职场文书
教师考核鉴定意见
2015/06/05 职场文书
MongoDB数据库的安装步骤
2021/06/18 MongoDB