方式一

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服务器打印信息的四种不同方式()的更多相关文章

  1. HTTP获取信息的四种方式

    HTTP 从网络获取信息的四种方式 GET GET指代你在浏览器中输入网址,浏览网站时做的事.例如,我们使用 http://www.baidu.com 的时候,可以将GET想象成他说:"hi ...

  2. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

  3. (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

  4. HttpwebClient的四种请求方式

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.      本文旨在发布代码,供自己参考,也供大家参考,谢谢. 正题: Ht ...

  5. xml常用四种解析方式优缺点的分析×××××

    xml常用四种解析方式优缺点的分析 博客分类: xml   最近用得到xml的解析方式,于是就翻了翻自己的笔记同时从网上查找了资料,自己在前人的基础上总结了下,贴出来大家分享下. 首先介绍一下xml语 ...

  6. c++ --> c++中四种类型转换方式

    c++中四种类型转换方式   c风格转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少缺点, 1)它可以在任意类型之间转换,比如你可以把一个指向const对象的指针转换成指向 ...

  7. Web.xml中四种验证方式

    源地址:https://blog.csdn.net/imimi_/article/details/78805642 <security-constraint> 的子元素 <http- ...

  8. [Android]Android数据的四种存储方式

    存储方式 Android提供以下四种存储方式: SharePreference SQLite File ContentProvider Android系统中数据基本都是私有的,一般存放在“data/d ...

  9. 【转】C++四种类型转换方式

    C++四种类型转换方式 https://blog.csdn.net/lv_amelia/article/details/79483579 C风格的强制类型转换(Type Case)很简单,不管什么类型 ...

随机推荐

  1. 雷林鹏分享:C# 类型转换

    C# 类型转换 类型转换从根本上说是类型铸造,或者说是把数据从一种类型转换为另一种类型.在 C# 中,类型铸造有两种形式: 隐式类型转换 - 这些转换是 C# 默认的以安全方式进行的转换.例如,从小的 ...

  2. one-class logistic regression (OCLR)

    ONE-CLASS DETECTION OF CELL STATES IN TUMOR SUBTYPES Machine Learning Identifies Stemness Features A ...

  3. caffe在win10下的安装与配置

    1.Windows环境caffe安装配置(无GPU) 参考:http://www.cnblogs.com/cxyxbk/p/5902034.html 解压caffe-windows文件,将./wind ...

  4. 安装Jade

    1.安装node.js 直接有安装程序:http://nodejs.cn/ 2.安装git 直接的安装程序:http://jingyan.baidu.com/article/90895e0fb3495 ...

  5. 说说secondarynamenode作用和配置

    说说secondarynamenode作用 http://my.oschina.net/u/1464779/blog/289895 说说secondarynamenode的配置 hadoop2.X如何 ...

  6. redis 持久化方式

    对于persistence持久化存储,Redis提供了两种持久化方法: Redis DataBase(简称RDB) 执行机制:快照,直接将databases中的key-value的二进制形式存储在了r ...

  7. python3 设置滚动条

    #!python3#coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.by import Byimpor ...

  8. laravel中常用的获取路径的函数

    1. app_path() // 获取app目录的路径 2. base_path() // 根目录的路径 3. config_path() // config目录的路径 4. public_path( ...

  9. 使用maven命令把jar包加入maven仓库

    命令:mvn install:install-file -Dfile=D:\jar包路径xxxx.jar -DgroupId=根目录文件夹名字  -DartifactId=子目录文件夹 -Dversi ...

  10. vue中使用transition标签底部导航闪烁问题

    <transition :name="transitionName" :duration="{ enter: 500, leave: 0 }" > ...