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服务端渲染>,主要说明了相关概念,以及为什么使用等,昨天的一个小栗子因为时间问题,没有好好的给大家铺开来讲 ...
随机推荐
- python类的__new__和__init__
python的类,和其他语言有一点不太一样,就是,他把新建一个类和初始化一个类,分成了两个方法: __new__ __init__ 当然,想想就知道,肯定是__new__先发生,然后才是__init_ ...
- 第二篇:zone(区域)
什么是区域? 网络区域定义网络连接的信任级别(the level of trust for network connections).一个网络连接只能是一个区域的一部分,但一个区域可以包含许多网络连接 ...
- eclipse怎么关闭spring dashboard
进入help-install new software-what is already installed?-卸载spring board
- hdu5790
都快忘了在这类题的经典做法了…… 将字符串一个个的插入字典树,在字典树维护好有该前缀串s的最大编号字符串j,我们记作j控制了前缀串s 对于当前的第i个字符串,维护此时有当前每个字符串控制了多少个前缀串 ...
- AC日记——魔方 洛谷 P2007
魔方 思路: 模拟: 代码: #include <cstdio> #include <cstring> #include <iostream> #include & ...
- 求给出第 K个 N位二进制数,该二进制数不得有相邻的“1”
求给出第 K (0 < K < 109) 个 N (0 < N < 44) 位二进制数,该二进制数不得有相邻的"1". 这道题要求给出第 K (0 < ...
- 【KM】POJ2195/HDU1533-Going home
//最近没什么时间quq据说长得帅的人都在切八中,然而长得丑的人只能水水裸题 [题目大意] 给出一张地图及人和房屋的位置,求出每个人回到不同房屋所具有的最小代价和. [思路] 最小权匹配,先O(n^2 ...
- Linux下提示命令找不到:bash:command not found
Linux下输入某些命令时会提示:bash:command not found. 首先,查看$PATH中是否包含了这些命令. $PATH:决定了shell到哪些目录中去寻找命令或程序,PATH值是一系 ...
- 1.4(Mybatis学习笔记)关联映射
一.一对一 mybatis处理一对一主要通过<resultMap>中的<association>元素来处理. <association>元素主要使用方方式有两种: ...
- Problem M: 第几天——C语言初学者百题大战之十八
#include<stdio.h> int main() { int a,b,c,s; scanf("%d-%d-%d",&a,&b,&c); ...