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服务端渲染>,主要说明了相关概念,以及为什么使用等,昨天的一个小栗子因为时间问题,没有好好的给大家铺开来讲 ...
随机推荐
- __rb_tree_rebalance
Inline void __rb_tree_rebalance(__rb_tree_node_base* x, __rb_tree_node_base*& root) //当前节点,根 { x ...
- eclipse中的project 和classpath文件的具体作用
项目->右键 Properties-> java Build Path -> source: xxx/data xxx/src 体现在 .classpath文件中: .classpa ...
- Linux命令之:tr
1. 用途: tr,translate的简写,主要用于压缩重复字符,删除文件中的控制字符以及进行字符转换操作. 2. 语法: tr [OPTION]... SET1 [SET2] 3. 参数: -s: ...
- 正则表达式、re、常用模块
阅读目录 正则表达式 字符 量词 . ^ $ * + ? { } 字符集[][^] 分组 ()与 或 |[^] 转义符 \ 贪婪匹配 re 总结 正则 re 常用模块 namedtuple deque ...
- React--基础学习混搭
最近学习一下React,通过 阮一峰<React 入门实例教程> .React 入门教程.菜鸟教程--React 这三个学习基础使用,接下来看慕课网的三个教学视频. React是什么我也 ...
- sublime text3下使用TAG快捷键ctrl+alt+f失效的解决方法
系统环境:WIN7 版本: sublime text3 问题:为了方便格式化html,下的TAG插件.在package control中在线安装能够安装成功,功能正常使用,就是ctrl+alt+f(A ...
- 洛谷 P1577 切绳子【二分答案】
题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位. 输入输出格式 输入格式: 第一行两个整数N和K,接下来N行,描 ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接:http://poj.org/problem?id=1240 本文链接:http://www.cnblogs.com/Ash-ly/p/5482520.html 题意: 通过一棵二叉树的中序 ...
- [CF235E]Number Challenge
$\newcommand{fl}[1]{\left\lfloor#1\right\rfloor}$题意:求$\sum\limits_{i=1}^a\sum\limits_{j=1}^b\sum\lim ...
- 【AC自动机】【动态规划】hdu2296 Ring
题解:http://www.cnblogs.com/swm8023/archive/2012/08/08/2627535.html 要输出路径,价值最大优先,价值相同的取长度较小者,仍相同取字典序较小 ...