Python基于Socket实现的简单聊天程序示例
作者:叶赫那拉坤 时间:2022-12-22 09:14:50
本文实例讲述了Python基于Socket实现的简单聊天程序。分享给大家供大家参考,具体如下:
需求:SCIENCE 和MOOD两个人软件专业出身,厌倦了大众化的聊天软件,想着自己开发一款简易的聊天软件,满足他们的个性化需求,又不失“专业水准”,Talk is easy, try to code it.
技术:socket,详细可参考前文:Python Socket实现简单TCP Server/client功能
语言:python
尽管socket区分服务器和客户端,但是在聊天程序中两者是平等的关系,都是客户端程序。
Server.py
import socket
host = socket.gethostname()
port = 12345
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(1)
sock,addr = s.accept()
print('Connection built')
info = sock.recv(1024).decode()
while info != 'exit':
print('MOOD:'+info)
send_mes = input()
sock.send(send_mes.encode())
if send_mes =='exit':
break
info = sock.recv(1024).decode()
sock.close()
s.close()
Client.py
import socket
s= socket.socket()
host = socket.gethostname()
port = 12345
s.connect((host,port))
print('Linked')
info = ''
while info != 'exit':
print('SCIENCE:'+info)
send_mes=input()
s.send(send_mes.encode())
if send_mes =='exit':
break
info = s.recv(1024).decode()
s.close()
效果截图:
恩,太low了有木有?连个图形化界面都没有,跟别谈其他个性化功能了。希望他们继续努力,早日做出属于他们自己的聊天软件吧!
希望本文所述对大家Python程序设计有所帮助。
标签:Python,Socket,聊天
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
django2.2安装错误最全的解决方案(小结)
2022-06-14 20:37:19
python小程序基于Jupyter实现天气查询的方法
2021-05-30 10:39:40
![](https://img.aspxhome.com/file/2023/1/112111_0s.jpg)
python集合的新增元素方法整理
2022-10-13 02:45:28
python两个_多个字典合并相加的实例代码
2023-05-05 07:50:33
python打印当前文件的绝对路径并解决打印为空的问题
2023-10-26 16:24:00
MySQL5创建存储过程实例
2010-06-13 12:49:00
Python subprocess模块常见用法分析
2023-05-22 18:48:44
Mysql导出数据的正确方法
2024-01-22 11:24:06
![](https://img.aspxhome.com/file/2023/7/116757_0s.jpg)
python3 实现的人人影视网站自动签到
2022-01-22 21:49:52
vue项目配置element-ui容易遇到的坑及解决
2024-04-29 13:10:25
![](https://img.aspxhome.com/file/2023/8/132908_0s.png)
python在不同层级目录import模块的方法
2023-02-05 01:45:03
Python datetime时间格式化去掉前导0
2022-03-15 11:43:15
js调用AJAX时Get和post的乱码解决方法
2024-05-09 10:38:11
不得不看的JS基础知识(事件触发篇)
2008-12-04 16:38:00
Python中enumerate()函数编写更Pythonic的循环
2023-06-09 14:01:29
如何用ASP创建日志文件
2008-03-10 17:27:00
微信小程序实现简单倒计时功能
2024-04-17 10:23:27
![](https://img.aspxhome.com/file/2023/4/136304_0s.jpg)
Node Express用法详解【安装、使用、路由、中间件、模板引擎等】
2024-05-11 10:49:24
用python打包exe应用程序及PyInstaller安装方式
2023-09-20 03:55:21
![](https://img.aspxhome.com/file/2023/8/133878_0s.png)
python MultipartEncoder传输zip文件实例
2022-04-20 07:22:57