# client 端
import socket ip_port = ('127.0.0.1', 8091)
sk = socket.socket()
sk.connect(ip_port)
print("客户端启动: ") while True:
inp = input(">>>")
sk.sendall(bytes(inp, "utf8"))
if inp == 'exit':
break
server_response = sk.recv(1024)
print(str(server_response, "utf8"))
sk.close() # server 端
import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self):
print("服务器启动...")
while True:
conn = self.request
print(self.client_address)
while True:
client_data = conn.recv(1024)
print(str(client_data, "utf8"))
print("waiting...")
server_response = input(">>>")
conn.sendall(bytes(server_response, "utf8"))
conn.close() if __name__ == '__main__':
server = socketserver.ThreadingTCPServer(('127.0.0.1',8091), MyServer)
server.serve_forever() # 这里会执行 handle 方法,所以 handle 方法里是编写程序逻辑。

创建一个socketserver 至少分以下几步

First, you must create a request handler class by subclassing the BaseRequestHandlerclass and overriding its handle() method; this method will process incoming requests.

  

Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class.

Then call the handle_request() or serve_forever() method of the server object to process one or many requests.

Finally, call server_close() to close the socket.

Python 简单的多线程聊天的更多相关文章

  1. Python 简单理解多线程

    进程,是一个或多个线程的集合,每个进程在内存中是相对独立的. 线程,是计算机最小的运算单元,每个进程至少要有一个线程,多个线程时,每个线程间之间共享内存. 分别举例常规运行和多线程运行: 0)常规运行 ...

  2. Python简单的多线程demo:常用写法

    简单多线程实现:启动50个线程,并计算执行时间. import threading import time def run(n): time.sleep(3) print("task:&qu ...

  3. JavaSE简单实现多线程聊天

    1.1 主程序入口 在主程序入口处,通过设置MyWindow的第一个参数,如果为true则为服务器,如果为false,则为客户端,当然也可以设置第二个参数,区分客户端和服务器的窗口标题. public ...

  4. Python简单的多线程demo:装逼写法

    用面向对象来写多线程: import threading class MyThread(threading.Thread): def __init__(self, n): super(MyThread ...

  5. 多任务-python实现-UDP多线程聊天(2.1.6)

    @ 目录 1.案例 1.案例 代码实现 import threading import time import socket def rev_msg(udp_socket): while True: ...

  6. 一个简单的多线程Python爬虫(一)

    一个简单的多线程Python爬虫 最近想要抓取拉勾网的数据,最开始是使用Scrapy的,但是遇到了下面两个问题: 前端页面是用JS模板引擎生成的 接口主要是用POST提交参数的 目前不会处理使用JS模 ...

  7. python 简单搭建阻塞式单进程,多进程,多线程服务

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 我们可以通过这样子的方式去理解apache的工作原理 1 单进程TCP服 ...

  8. 【Python网络编程】多线程聊天软件程序

    课程设计的时候制作的多线程聊天软件程序 基于python3.4.3 import socket import pickle import threading import tkinter import ...

  9. python 全栈开发,Day130(多玩具端的遥控功能, 简单的双向聊天,聊天记录存放数据库,消息提醒,玩具主动发起消息,玩具主动发起点播)

    先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.3.zip 注意:由于涉及到 ...

随机推荐

  1. 微服务ServiceMesh及三种模式介绍

    1. 前言 今年,ServiceMesh(服务网格)概念在社区里头非常火,有人提出2018年是ServiceMesh年,还有人提出ServiceMesh是下一代的微服务架构基础.作为架构师,如果你现在 ...

  2. 请教:WCF速度似乎比Remoting慢

    两段极为相似的代码,主要想看看过输与序列化过程两者的用时差异,结果10000次的调用,WCF用了11秒多,remoting用了5秒不到!这是测试的源代码 Remoting的服务端 public cla ...

  3. Java字符串池(String Pool)深度解析

    版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! 在工作中,String类是我们使用频率非常高的一种对象类型.JVM为了提升性能和减少内存开销,避免字符串的重复创建,其维护了一块特殊的内存 ...

  4. 项目Alpha冲刺(团队4/10)

    项目Alpha冲刺(团队4/10) 团队名称: 云打印 作业要求: 项目Alpha冲刺(团队) 作业目标: 完成项目Alpha版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 ...

  5. 读入挂(IO)

    快如闪电,清华杜瑜皓的读入挂,一模一样代码,加了这个之后... 细思极恐,and 整整行!!! namespace IO{ #define BUF_SIZE 100000 #define OUT_SI ...

  6. video设置autoplay 不起作用

    video 标签上添加 muted https://blog.csdn.net/taiyangmiaomiao/article/details/80266625

  7. Android之控件与布局,结构知识点,基础完结

    版权声明:未经博主允许不得转载 在Android中我们常常用到很多UI控件,如TextView,EditText,ImageView,Button,ImageButton,ToggleButton,C ...

  8. OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 3.安装配置OpenStack认证服务(keystone)

    节点配置信息说明: 控制节点:controller: IP:192.168.164.128 hostname&hosts:likeadmin 计算加点:Nova: IP:192.168.164 ...

  9. Python语法基础——关于全局变量与局部变量

    1.函数内部的变量名如果第一次出现,且出现在=前面,即被视为定义一个局部变量,不管全局域中有没有用到该变量名,函数中使用的将是局部变量,例如: num = 100 def func(): num =  ...

  10. Spark架构

    Spark架构    为了更好地理解调度,我们先来鸟瞰一下集群模式下的Spark程序运行架构图. 1. Driver Program        用户编写的Spark程序称为Driver Progr ...