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服务端渲染>,主要说明了相关概念,以及为什么使用等,昨天的一个小栗子因为时间问题,没有好好的给大家铺开来讲 ...
随机推荐
- Java上传文件格式判断
判断用户上传文件的合法性仅仅通过后缀名是完全不够的,谁也不知道后缀名是否被更改,服务器保存一个不知道真实类型的文件有极大的风险. 因此需要后台进行进一步的文件类型校验,这里有两种情况: 1)一般的文件 ...
- Selenium2+python自动化70-unittest之跳过用例(skip)【转载】
前言 当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例 ...
- Django基本操作
Django官网下载页面 安装(安装最新LTS版): pip3 install django==1.11.9 创建一个django项目: 下面的命令创建了一个名为"s8"的Djan ...
- 厦门大学XMUNET+ ubuntu连接方法
编辑连接 输入用户名和密码就好了 ubuntu16.04
- Centos7重置root密码的方法(亲测有效)
CentOS 7的更新还是非常大的,很多平时使用的命令已经变化了,要上手还真要一段时间.比如忘记root密码.在5.6的系统中直接进入单用户模式下,一个passwd命令修改,重启即可.但是在Cento ...
- CentOS系统挂载FAT32的U盘
Linux挂载U盘步骤如下 1:将U盘插入USB接口,检查是否插好 2:用fdisk命令检查分区和USB设备信息 [root@wgods ~]# fdisk -l Disk /dev/sda: 100 ...
- 【cocos2d-js官方文档】二十一、v3相对于v2版本的api变动
分类: cocos2d-js(28) 目录(?)[+] CCAudio.js SimpleAudioEngine.js改名为CCAudio.js. AudioEngine中删除了以下几个方法: pre ...
- System Center VMM请注意不同语言版本的差异
在私有云的项目中,经常需要判断System Center一些组件的连接是否OK. 我这里有开发,和测试两个环境,开发是英文版的System Center VMM,测试用的是中文版的System Cen ...
- 卡特兰数 3134 Circle
3134 Circle 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在一个圆上,有2*K个不同的结点,我们 ...
- RPD Volume 168 Issue 4 March 2016 评论3
Design and fabrication of a multipurpose thyroid phantom for medical dosimetry and calibration Abs ...