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)很简单,不管什么类型 ...
随机推荐
- 把 java web application deploy 到 root
http://stackoverflow.com/questions/5328518/deploying-my-application-at-the-root-in-tomcat You have a ...
- a gcc 4.2.4 bug(被stos指令累加后%edi作为参数的)
a gcc 4.2.4 bug(被stos指令累加后%edi作为参数的) * * Ok, now we can initialize the rest of the tty devices and c ...
- Confluence 6 让一个空间可以公众访问
如果你希望将一个空间分享给没有登录 Confluence 的用户(匿名用户)可以访问的话,你需要将这个空间标记为 公开(public). 让一个空间可以公开的访问的话,你就不能选择如何对这个空间进行 ...
- 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- XXE漏洞
原理:XML外部实体注入,简称XXE漏洞,XML数据在传输中数据被修改,服务器执行被恶意插入的代码.当允许引用外部实体时,通过构造恶意内容,就可能导致任意文件读取.系统命令执行.内网端口探测.攻击内网 ...
- 二元谓词中添加const的问题(未解决)
#include <iostream> using namespace std; #include"set" #include"algorithm" ...
- python-mongodb基本操作都在这了
数据库 增 use db1 #有则切换,无则新增 查 show dbs #查看所有 db #当前 删 db.dropDatabase() 集合: 增: db.user db.user.info db. ...
- php调用oracle存储
//todo 调用oracle 存储$config = //数据库配置文件 里面包含 用户密码和host和端口以及dbname $conn = oci_connect($config['usernam ...
- hpu_newoj_1028-exgcd
The Elevator 描述 全是电梯. Philo正处于高度为0的一个平台上,在他面前的一个平面,全是上上下下的电梯. Philo想要离开这里,请你帮帮他. 电梯世界规则:这里的电梯所能到达的 ...
- poj-2115-exgcd
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32062 Accepted: 9337 Descr ...