python使用多线程编写tcp客户端程序


Posted in Python onSeptember 02, 2019

今天在网上找了半天,发现很多关于此题目的程序都只能接收数据,所以随便找了个程序研究了一下,然后做出一些修改

代码如下:

from socket import *
import threading
tcp_socket = socket(AF_INET, SOCK_STREAM)
tcp_socket.connect(('192.168.1.102', 8080))
true = True


def rece_msg(tcp_socket):
 global true
 while true:
  recv_msg = tcp_socket.recv(1024).decode("utf8")
  if recv_msg == "exit":
   true = False
  print('接收到的信息为:%s' % recv_msg)


def send_msg(tcp_socket):
 global true
 while true:
  send_msg = input('请输入要发送的内容')
  tcp_socket.send(send_msg.encode('utf-8'))
  if send_msg == "exit":
   true = False


def main():
 while True:
  print('*'*50)
  print('1 发送消息\n2 接收消息')
  option = int(input('请选择操作内容'))
  print('*'*50)
  if option == 1:
   threading.Thread(target=send_msg, args=(tcp_socket,)).start()
  elif option == 2:
   threading.Thread(target=rece_msg, args=(tcp_socket,)).start()
  else:
   print('输入有误')
  break


if __name__ == '__main__':
 main()

该代码只能实现要么一直发送,要么一直接收

运行如图

发送数据时截图

 python使用多线程编写tcp客户端程序

python使用多线程编写tcp客户端程序

接收数据时截图

 python使用多线程编写tcp客户端程序

python使用多线程编写tcp客户端程序

为解决只能单方发送和接收问题,现将代码修改如下

from socket import *
import threading
tcp_socket = socket(AF_INET, SOCK_STREAM)
tcp_socket.connect(('192.168.1.102', 8080))
true = True


def rece_msg(tcp_socket):
 global true
 while true:
  recv_msg = tcp_socket.recv(1024).decode("utf8")
  if recv_msg == "exit":
   true = False
  print('接收到的信息为:%s\n' % recv_msg)


def send_msg(tcp_socket):
 global true
 while true:
  send_msg = input('请输入要发送的内容\n')
  tcp_socket.send(send_msg.encode('utf-8'))
  if send_msg == "exit":
   true = False


threading.Thread(target=send_msg, args=(tcp_socket,)).start()
threading.Thread(target=rece_msg, args=(tcp_socket,)).start()

运行结果

python使用多线程编写tcp客户端程序

python使用多线程编写tcp客户端程序

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

Python 相关文章推荐
Python脚本判断 Linux 是否运行在虚拟机上
Apr 25 Python
为Python的Tornado框架配置使用Jinja2模板引擎的方法
Jun 30 Python
python使用pil库实现图片合成实例代码
Jan 20 Python
python实现扫描日志关键字的示例
Apr 28 Python
python实现连续图文识别
Dec 18 Python
pyinstaller参数介绍以及总结详解
Jul 12 Python
pycharm双击无响应(打不开问题解决办法)
Jan 10 Python
基于Pytorch SSD模型分析
Feb 18 Python
解决jupyter notebook import error但是命令提示符import正常的问题
Apr 15 Python
如何基于python实现不邻接植花
May 01 Python
python 视频下载神器(you-get)的具体使用
Jan 06 Python
Python3利用openpyxl读写Excel文件的方法实例
Feb 03 Python
使用python实现离散时间傅里叶变换的方法
Sep 02 #Python
详解Python图像处理库Pillow常用使用方法
Sep 02 #Python
Django使用中间件解决前后端同源策略问题
Sep 02 #Python
python elasticsearch环境搭建详解
Sep 02 #Python
关于pymysql模块的使用以及代码详解
Sep 01 #Python
使用Python将字符串转换为格式化的日期时间字符串
Sep 01 #Python
Python 使用多属性来进行排序
Sep 01 #Python
You might like
PHP实现根据数组的值进行分组的方法
2017/04/20 PHP
Yii框架中用response保存cookie,用request读取cookie的原理解析
2019/09/04 PHP
laravel实现前后台路由分离的方法
2019/10/13 PHP
javascript 操作文件 实现方法小结
2009/07/02 Javascript
js进行表单验证实例分析
2015/02/10 Javascript
浅谈JavaScript中指针和地址
2015/07/26 Javascript
深入理解node exports和module.exports区别
2016/06/01 Javascript
JS实现六位字符密码输入器功能
2016/08/19 Javascript
javascript实现的上下无缝滚动效果
2016/09/19 Javascript
jquery html5 视频播放控制代码
2016/11/06 Javascript
Node.js中 __dirname 的使用介绍
2017/06/19 Javascript
JavaScript字符串检索字符的方法
2017/06/23 Javascript
基于jQuery实现Ajax验证用户名是否可用实例
2018/03/25 jQuery
解决layui的radio属性或别的属性没显示出来的问题
2019/09/26 Javascript
JS精确判断数据类型代码实例
2019/12/18 Javascript
Openlayers显示地理位置坐标的方法
2020/09/28 Javascript
[01:45]绝对公平!DOTA2队长征召模式详解
2014/04/25 DOTA
[11:57]《一刀刀一天》第十七期:TI中国军团加油!
2014/05/26 DOTA
Python装饰器使用实例:验证参数合法性
2015/06/24 Python
python flask实现分页效果
2017/06/27 Python
5个很好的Python面试题问题答案及分析
2018/01/19 Python
python使用json序列化datetime类型实例解析
2018/02/11 Python
使用python批量修改文件名的方法(视频合并时)
2020/03/24 Python
Python for循环通过序列索引迭代过程解析
2020/02/07 Python
解决Python安装cryptography报错问题
2020/09/03 Python
matplotlib交互式数据光标mpldatacursor的实现
2021/02/03 Python
python 统计list中各个元素出现的次数的几种方法
2021/02/20 Python
毕业生找工作的求职信范文
2013/12/24 职场文书
如何写你的创业计划书
2014/01/07 职场文书
奥巴马获胜演讲稿
2014/05/15 职场文书
初一新生军训方案
2014/05/22 职场文书
学校党委干部个人对照检查材料思想汇报
2014/10/09 职场文书
详解前端任务构建利器Gulp.js使用指南
2021/04/30 Javascript
世界十大动漫制作公司排行榜,迪士尼上榜,第二是美国代表性文化符
2022/03/18 欧美动漫
springboot 自定义配置 解决Boolean属性不生效
2022/03/18 Java/Android
win10频率超出范围怎么办?win10老显示超出工作频率范围的解决方法
2022/07/07 数码科技