# 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. Hessian 使用例子

    一.协议包(数据对象需要实现序列化接口,可以用于服务端接口.客户端调用服务之用) /** * */ package com.junge.demo.protocol.model; import java ...

  2. 人脸识别准备 -- 基于raspberry pi 3b + movidius

    最近准备系统地学习一下深度学习和TensorFlow,就以人脸识别作为目的. 十年前我做过一些图像处理相关的项目和研究,涉及到图像检索.记得当时使用的是SIFT特征提取,该特征算子能很好地抵抗图像旋转 ...

  3. Programming Specification

    1. Define variable return_code to record the function's status. int return_code = 0; 2. Define the e ...

  4. [SRC初探]手持新手卡挖SRC逻辑漏洞心得分享

    文章来源i春秋 本文适合新手参阅,大牛笑笑就好了,嘿嘿末尾有彩蛋!!!!!!!!!!!!!!!!!本人参加了本次"i春秋部落守卫者联盟"活动,由于经验不足,首次挖SRC,排名不是那 ...

  5. WCF绑定netTcpBinding寄宿到IIS

    继续沿用上一篇随笔中WCF服务类库 Wettery.WcfContract.Services WCF绑定netTcpBinding寄宿到控制台应用程序 服务端 添加WCF服务应用程序 Wettery. ...

  6. Dispatch Queue 之 dispatch_sync

  7. Virtualbox+Vagrant环境准备

    环境准备 所需软件 下载地址 VirtualBox-5.2.8-121009-Win https://download.virtualbox.org/virtualbox/5.2.12/Virtual ...

  8. 从零开始学 Spring Boot

    1.下载 spring-tool-suite https://spring.io/tools3/sts/legacy 2.解压运行 sts-bundle\sts-3.9.7.RELEASE\STS.e ...

  9. Spark SQL 性能优化再进一步:CBO 基于代价的优化

    摘要: 本文将介绍 CBO,它充分考虑了数据本身的特点(如大小.分布)以及操作算子的特点(中间结果集的分布及大小)及代价,从而更好的选择执行代价最小的物理执行计划,即 SparkPlan. Spark ...

  10. [Umbraco] umbraco中如何分页

    分页功能应该说是web开发中最基本的功能了,常规的做法是通过查询sql语句进行分页数据显示.但在umbraco中却不是这样子的.而且通过xpath中的postion来定位.如下代码 <?xml ...