Python 简单的多线程聊天
# 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 简单的多线程聊天的更多相关文章
- Python 简单理解多线程
进程,是一个或多个线程的集合,每个进程在内存中是相对独立的. 线程,是计算机最小的运算单元,每个进程至少要有一个线程,多个线程时,每个线程间之间共享内存. 分别举例常规运行和多线程运行: 0)常规运行 ...
- Python简单的多线程demo:常用写法
简单多线程实现:启动50个线程,并计算执行时间. import threading import time def run(n): time.sleep(3) print("task:&qu ...
- JavaSE简单实现多线程聊天
1.1 主程序入口 在主程序入口处,通过设置MyWindow的第一个参数,如果为true则为服务器,如果为false,则为客户端,当然也可以设置第二个参数,区分客户端和服务器的窗口标题. public ...
- Python简单的多线程demo:装逼写法
用面向对象来写多线程: import threading class MyThread(threading.Thread): def __init__(self, n): super(MyThread ...
- 多任务-python实现-UDP多线程聊天(2.1.6)
@ 目录 1.案例 1.案例 代码实现 import threading import time import socket def rev_msg(udp_socket): while True: ...
- 一个简单的多线程Python爬虫(一)
一个简单的多线程Python爬虫 最近想要抓取拉勾网的数据,最开始是使用Scrapy的,但是遇到了下面两个问题: 前端页面是用JS模板引擎生成的 接口主要是用POST提交参数的 目前不会处理使用JS模 ...
- python 简单搭建阻塞式单进程,多进程,多线程服务
由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 : --> 点击这里 我们可以通过这样子的方式去理解apache的工作原理 1 单进程TCP服 ...
- 【Python网络编程】多线程聊天软件程序
课程设计的时候制作的多线程聊天软件程序 基于python3.4.3 import socket import pickle import threading import tkinter import ...
- python 全栈开发,Day130(多玩具端的遥控功能, 简单的双向聊天,聊天记录存放数据库,消息提醒,玩具主动发起消息,玩具主动发起点播)
先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.3.zip 注意:由于涉及到 ...
随机推荐
- EBS server log日志
Log file location for Oracle E-Business Suite R12 April 13, 2010 in Oracle Database&EBS General ...
- Scrum 冲刺博客集合
Day1 博客链接:http://www.cnblogs.com/coolgirls/p/8869839.html Day2 博客链接:http://www.cnblogs.com/coolgirls ...
- Windows核心编程:第1章 错误处理
Github https://github.com/gongluck/Windows-Core-Program.git //第1章 错误处理.cpp: 定义应用程序的入口点. // #include ...
- DOM扩展:DOM API的进一步增强[总结篇-上]
DOM1级主要定义了文档的底层结构,并提供了基本的查询操作的API,总体而言这些API已经比较完善,我们可以通过这些API完成大部分的DOM操作.然而,为了扩展DOM API的功能,同时进一步提高DO ...
- 用react+redux写一个todo
概述 最近学习redux,打算用redux写了一个todo.记录下来,供以后开发时参考,相信对其他人也有用. 代码 代码请见我的github 组织架构如下图:
- Nginx 负载均衡与反向代理
通过设置权重来轮询 weight server 192.168.1.62 weight=5 server 192.168.63 weight=1 ip_hash 第3方均衡策略 fair url_h ...
- Caffe 使用记录(五):math_functions 分析
本文转载自 Caffe源码(一):math_functions 分析 math_function 定义了caffe 中用到的一些矩阵操作和数值计算的一些函数,这里以float类型为例做简单的分析 1. ...
- python学习记录(一)
1.打印操作 >>> print('hello') hello >>> print(1+2) 3 2.字符串操作 ① ') Traceback (most rece ...
- 1-VScode格式化ESlint-方法(最全最好用方法!)
1-VScode格式化ESlint-方法(最全最好用方法!) ESlint:是用来统一JavaScript代码风格的工具,不包含css.html等. 背景: 近来研究前端,然后一直在百度上找VSc ...
- numpy中pad函数的常用方法
一.参数解释 ndarray = numpy.pad(array, pad_width, mode, **kwargs) array为要填补的数组 pad_width是在各维度的各个方向上想要填补的长 ...