1.4socket服务器打印信息的四种不同方式()
方式一
socker 服务器
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def initListenerSocket(port=port):
sock = socket(AF_INET,SOCK_STREAM) #创建TCP对象
sock.bind(('',port)) #绑定端口
sock.listen(5) #允许5个请求连接
conn,addr = sock.accept() #返回socket对象
return conn def server1():
mypid = os.getpid() #获取系统进程
conn = initListenerSocket()
file = conn.makefile('r') #file interface wrapper
for i in range(3):
data = file.readline().rstrip() #读取之前客户端写入file的值
print('Sever %s:%s'%(mypid,data)) #当makefile('r')中为'r'时候,此时显示的是print值
server1()
socket 客户端
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def redirectOut(port=serverPort, host=serverHost):
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((host, port))
file = sock.makefile('w')
sys.stdout = file #此时sys.stdout指向file,原始的 sys.stdout 指向控制台,如果把文件的对象的引用赋给 sys.stdout,那么 print 调用的就是文件对象的 write 方法
return sock def client1():
mypid = os.getpid()
redirectOut()
for i in range(3):
print('Client %s:%s+++++'%(mypid,i)) #当makefile('w')中为'w'时候,此时print不能打印,print后为写入file中的值
sys.stdout.flush() #每一次print都会输入刷新 client1()
方式二
socker 服务器
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def initListenerSocket(port=port):
sock = socket(AF_INET,SOCK_STREAM) #创建TCP对象
sock.bind(('',port)) #绑定端口
sock.listen(5) #允许5个请求连接
conn,addr = sock.accept() #返回socket对象
return conn def server2():
mypid = os.getpid()
conn = initListenerSocket()
for i in range(3):
conn.send(('Sever %s:%s'%(mypid,i)).encode()) #和下面的注释功能一样,但是下面不报EOF错误
# ofile = conn.makefile('w')
# sys.stdout=ofile
# print(('Sever %s:%s'%(mypid,i)).encode()) server2()
socker 客户端
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def redirectIn(port=serverPort, host=serverHost):
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((host, port))
file = sock.makefile('r')
sys.stdin = file
return sock def client2():
mypid = os.getpid()
redirectIn()
for i in range(3):
data = input() #input获取的是服务器发送过来的内容
print('Client %s got [%s]'%(mypid,data)) #打印信息,出现EOF错误,不知道为什么 client2()
方式三
socker 服务器
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def initListenerSocket(port=port):
sock = socket(AF_INET,SOCK_STREAM) #创建TCP对象
sock.bind(('',port)) #绑定端口
sock.listen(5) #允许5个请求连接
conn,addr = sock.accept() #返回socket对象
return conn def server3():
mypid = os.getpid()
conn = initListenerSocket()
file = conn.makefile('r') #读取之前客户端写入file的值
for i in range(3):
data = file.readline().rstrip()
conn.send(('Server %s got[%s]\n'%(mypid,data)).encode()) server3()
socker 客户端
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def redirectBothAsClient(port=serverPort,host=serverHost):
sock = socket(AF_INET,SOCK_STREAM)
sock.connect((host,port))
ofile = sock.makefile('w')
ifile = sock.makefile('r')
sys.stdout = ofile
sys.stdin = ifile
return sock def client3():
mypid = os.getpid()
redirectBothAsClient()
for i in range(3):
print('Client %s: %s'%(mypid,i)) #写入ofile,再传到服务器
data = input() #再传回来
sys.stderr.write('Client %s got [%s]\n'%(mypid,data)) #打印出来,这里不能用print打印
#print('Client %s got [%s]\n'%(mypid,data)) client3()
方式四
socker 服务器
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def initListenerSocket(port=port):
sock = socket(AF_INET,SOCK_STREAM) #创建TCP对象
sock.bind(('',port)) #绑定端口
sock.listen(5) #允许5个请求连接
conn,addr = sock.accept() #返回socket对象
return conn def server5():
mypid = os.getpid()
conn = initListenerSocket()
file = conn.makefile('r')
for i in range(3):
conn.send(('Server %s got[%s]\n' % (mypid,i)).encode())
data = file.readline().rstrip()
print(('Server %s got[%s]\n' % (mypid, data))) server5()
socker 客户端
# -*- coding: utf-8 -*-
import sys,os,multiprocessing
from socket import * serverHost = 'localhost'
serverPort = 50007 def redirectBothAsClient(port=serverPort,host=serverHost):
sock = socket(AF_INET,SOCK_STREAM)
sock.connect((host,port))
ofile = sock.makefile('w')
ifile = sock.makefile('r')
sys.stdout = ofile
sys.stdin = ifile
return sock def client5():
mypid = os.getpid()
s = redirectBothAsClient()
for i in range(3):
data = input()
print('Client %s got [%s]\n' % (mypid, data))
sys.stdout.flush() #每一次print都会输入刷新 client5()
1.4socket服务器打印信息的四种不同方式()的更多相关文章
- HTTP获取信息的四种方式
HTTP 从网络获取信息的四种方式 GET GET指代你在浏览器中输入网址,浏览网站时做的事.例如,我们使用 http://www.baidu.com 的时候,可以将GET想象成他说:"hi ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- HttpwebClient的四种请求方式
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷. 本文旨在发布代码,供自己参考,也供大家参考,谢谢. 正题: Ht ...
- xml常用四种解析方式优缺点的分析×××××
xml常用四种解析方式优缺点的分析 博客分类: xml 最近用得到xml的解析方式,于是就翻了翻自己的笔记同时从网上查找了资料,自己在前人的基础上总结了下,贴出来大家分享下. 首先介绍一下xml语 ...
- c++ --> c++中四种类型转换方式
c++中四种类型转换方式 c风格转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少缺点, 1)它可以在任意类型之间转换,比如你可以把一个指向const对象的指针转换成指向 ...
- Web.xml中四种验证方式
源地址:https://blog.csdn.net/imimi_/article/details/78805642 <security-constraint> 的子元素 <http- ...
- [Android]Android数据的四种存储方式
存储方式 Android提供以下四种存储方式: SharePreference SQLite File ContentProvider Android系统中数据基本都是私有的,一般存放在“data/d ...
- 【转】C++四种类型转换方式
C++四种类型转换方式 https://blog.csdn.net/lv_amelia/article/details/79483579 C风格的强制类型转换(Type Case)很简单,不管什么类型 ...
随机推荐
- Go语言学习之3 流程控制、函数
主要内容: 1. strings和strconv使用2. Go中的时间和日期类型3. 指针类型4. 流程控制5. 函数详解 1. strings和strconv使用 //strings . strin ...
- Spring Boot入门第四天:使用Thymeleaf模板引擎
原文链接 关于Thymeleaf的优点,我只说一条:它就是html页面啊,直接可以用浏览器打开.受够了JSP的同学可以尝试一下. 1.在pom.xml文件中添加依赖: <!--<depen ...
- Debian初始化配置
1.解决中文显示乱码windows的宋体文件上传到debian的字体目录,并运行dpkg-reconfigure locales命令来设置系统的字体root@debian:~# mv simsun.t ...
- 抽离amazeUI里面的弹出框
花了一些时间读了amazeUI的源码 把他的弹出框给单独抽离出来了,具体可以见源码:http://pan.baidu.com/s/1mibQ9T2
- BFS+二进制状态压缩 hdu-1429
好久没写搜索题了,就当练手吧. vis[][][1025]第三个维度用来维护不同key持有状态的访问情况. 对于只有钥匙没有对应门的位置,置为'.',避免不必要的状态分支. // // main.cp ...
- html标签积累
<marquee>滚动标签 <marquee>标签,它是成对出现的标签,首标签<marquee>和尾标签</marquee>之间的内容就是滚动内容.&l ...
- HDU 5710 Digit Sum
Let S(N)S(N) be digit-sum of NN, i.e S(109)=10,S(6)=6S(109)=10,S(6)=6. If two positive integers a,ba ...
- Spring配置表友好性优化思路
Spring配置表需要尽量保证对程序员的友好性,一下提供一种优化思路. 中途未保存,心态炸了,只贴图了,fuuuuuuuuuuuuuck 第一种(最烂,最不友好):以Json的格式保存在配置表中,程序 ...
- BigDecimal 类型数据比较大小
public static void main( String[] args ) { BigDecimal a=new BigDecimal(-1); if(a.compareTo(BigDecima ...
- Leetcode 98
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...