Python CODE——Nonblocking I/O client AND Delaying Server
#!Nonblocking I/O - Chapter 5 -pollclient.py
import socket,sys,select
port=51423
host='localhost' spinsize=10
spinpos=0
spindir=1 def spin():
global spinsize,spinpos,spindir
spinstr='.'*spinpos+'|'+'.'*(spinsize-spinpos-1)
sys.stdout.write('\r'+spinstr+' ')
sys.stdout.flush() spinpos+=spindir
if spinpos<0:
spindir=1
spinpos=1
elif spinpos>=spinsize:
spinpos-=2
spindir=-1 sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((host,port)) p=select.poll()
p.register(sock.fileno(),select.POLLIN|select.POLLERR|select.POLLHUP) while True:
results=p.poll(2002)
if len(results):
if results[0][1]==select.POLLIN:
data=sock.recv(4096).decode()
if not len(data):
print("\rRemote end closed connection; exiting.")
break
sys.stdout.write("\rReceived :"+data)
sys.stdout.flush()
spin()
else:
print("\rProblem occurred; exiting")
sys.exit(0)
#!Delaying Server - Chapter 5 -delayserver.py
import socket,traceback,time host=""
port=51423 sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind((host,port))
sock.listen(1) while True:
try:
clientsock,clientaddr= sock.accept()
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
continue try:
print("Got connection from ",clientsock.getpeername)
while True:
try:
print(time.asctime().encode())
clientsock.sendall(time.asctime().encode()+b"\n")
except:
break
time.sleep(2)
except (KeyboardInterrupt,SystemExit):
raise
except:
traceback.print_exc() try:
clientsock.close()
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
Python CODE——Nonblocking I/O client AND Delaying Server的更多相关文章
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...
- Exploring Python Code Objects
Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Ins ...
- 机器学习算法实现(R&Python code)
Machine Learning Algorithms Machine Learning Algorithms (Python and R) 明天考试,今天就来简单写写机器学习的算法 Types Su ...
- How to run Python code from Sublime
How to run Python Code from Sublime,and How to run Python Code with input from sublime Using Sublime ...
- Python code 提取UML
Python是一门支持面向对象编程的语言,在大型软件项目中,我们往往会使用面向对象的特性去组织我们的代码,那有没有这样一种工具,可以帮助我们从已有代码中提取出UML图呢?答案是有的.以下,我们逐个介绍 ...
- PEP 8 – Style Guide for Python Code
原文:PEP 8 – Style Guide for Python Code PEP:8 题目:Python代码风格指南 作者:Guido van Rossum, www.yszx11.cnBarry ...
- Change the environment variable for python code running
python程序运行中改变环境变量: Trying to change the way the loader works for a running Python is very tricky; pr ...
- Java虚拟机6:内存溢出和内存泄露、并行和并发、Minor GC和Full GC、Client模式和Server模式的区别
前言 之前的文章尤其是讲解GC的时候提到了很多的概念,比如内存溢出和内存泄露.并行与并发.Client模式和Server模式.Minor GC和Full GC,本文详细讲解下这些概念的区别. 内存溢出 ...
- 从壹开始前后端分离 [ Vue2.0+.NetCore2.1] 二十六║Client渲染、Server渲染知多少{补充}
前言 书接上文,昨天简单的说到了 SSR 服务端渲染的相关内容<二十五║初探SSR服务端渲染>,主要说明了相关概念,以及为什么使用等,昨天的一个小栗子因为时间问题,没有好好的给大家铺开来讲 ...
随机推荐
- "个性化空间"性能优化方案设计初步
一.问题的提出 在九月中开始,我们要打造个性化空间,领导要求的是只进行原型的设计,逻辑的设计,不进行技术开发.其实是严重不正确的,因为个性化空间其特点与现有的技术模型完全不同,现有的技术方案未必能适应 ...
- hit2739
好题,回路的问题一般都要转化为度数来做若原图的基图不连通,或者存在某个点的入度或出度为0则无解.统计所有点的入度出度之差di对于di>0的点,加边(s,i,di,0):对于di<0的点,加 ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- Linux环境安装python3
linux 安装Python3 1.python下载 请在终端输入如下命令: cd /home wget http://cdn.npm.taobao.org/dist/python/3.6.5/Pyt ...
- 福州3中集训day5
数论,zld神犇认为我们都学过数论的,讲了一波高端(入门?)操作,从扩展欧几里得开始,同余方程诸如此类,早晚得重修.连课件都没,拿着画图讲了一上午 sro_zld_orz 具体内容都记在本上. 还是说 ...
- 欧拉定理【p4861】按钮
Background Ada被关在了一个房间里. Description 房间的铁门上有一个按钮,还有一个显示屏显示着"1". 旁边还有一行小字:"这是一个高精度M进制计 ...
- Diamond Collector (动态规划)
问题 I: Diamond Collector 时间限制: 1 Sec 内存限制: 64 MB提交: 22 解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, alwa ...
- 北京DAY1下午
省选模拟题 周子凯 题目概况 中文题目名 简易比特币 计算 路径 英文题目名 bit calculation Path 输入文件名 bit.in calculation.in path.in 输出文件 ...
- 【pb_ds】【平衡树启发式合并】【并查集】bzoj2733 [HNOI2012]永无乡
用并查集维护联通性.对每个联通块维护一个平衡树.合并时启发式合并.比较懒,用了pb_ds. #include<cstdio> #include<ext/pb_ds/assoc_con ...
- 1.6(学习笔记)EL表达式
1.表达式输出属性 先来看一个简单的表达式小例子 el.jsp <%@ page language="java" contentType="text/html; c ...