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()