方式一

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. linux 下设置定时任务

    Linux Crontab 定时任务 命令详解 在工作中需要数据库在每天零点自动备份所以需要建立一个定时任务.我选择在Linux下使用Crontab来添加定时任务执行shell文件.shell文件有数 ...

  2. 转载:删除github上文件夹的两种方式

    http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...

  3. android主流开源库

    网络框架:Volley 和 Async Volley特点:能使网络通信更快,更简单.更健壮 Get,Post网络请求及网络图像的高效率 Async:高效的网络数据请求, 解析成json 持久化cook ...

  4. VS Code插件

    VS Code下载地址: https://code.visualstudio.com/ 1.view in browser   和  Open-In-Browser  安装可在编辑器中打开html,在 ...

  5. Python操作excel的几种方式--xlrd、xlwt、openpyxl

    openpyxl xlrd xlwt   在处理excel数据时发现了xlwt的局限性–不能写入超过65535行.256列的数据(因为它只支持Excel 2003及之前的版本,在这些版本的Excel中 ...

  6. selenium配置Chrome驱动

    1.http://chromedriver.storage.googleapis.com/index.html   chrome下载驱动地址 和对应的版本驱动,不用FQ 2.配置方法:如在e盘创建一个 ...

  7. js 异步加载

    document 加载 document.write("<scr" + "ipt src=\"js/jquery.js\"></sc ...

  8. 原生JS的地区二级联动,很好理解的逻辑

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 函数使用十二:BAPI_MATERIAL_BOM_GROUP_CREATE(CS61)

    REPORT ZSM_CREATE_SIMPLEBOM.* This code will create a material BoM for the material* MAINMATERIAL wi ...

  10. H5 页面在微信端的分享

    微信分享,咋一看好像很复杂,实则非常简单.只需要调用微信官方出的微信jssdk,加上些许配置,就可以实现h5页面在微信上的分享,官方文档地址为: https://mp.weixin.qq.com/wi ...