http://www.cnblogs.com/alex3714/articles/5830365.html 可以看的内容
socket sever
#! /usr/bin/env python # -*- coding:utf-8 -*- # Author Ian Ying # mail: [email protected] import socket import os server = socket.socket() server.bind((‘localhost‘, 6969)) #绑定被监听端口 server.listen(5) #监听端口 while True: print("我要开始等电话了") conn, addr = server.accept() # 就是等待的意思 #conn就是客户端连过来的时候,在服务器端为其生成的一个连接实例 print("电话来了%s"% [conn, addr]) while True: data = conn.recv(1024) if not data: print(‘client is lost.‘) break # res = os.popen(data).read() #popen就是打开命令执行,read就是获取结果 # with open(‘filename‘, ‘r‘) as ret: #这两行就 可以用过来传输文件了。 # data = ret.read() print(‘receive:‘,data) conn.send(data.upper()) server.close()
socket client 模块
#! /usr/bin/env python # -*- coding:utf-8 -*- # Author Ian Ying # mail: [email protected] import socket client = socket.socket() #声明socket类型,同时生成socket链接对象 client.connect((‘localhost‘,6969)) #localhost就是本机地址 while True: msg = input(‘input msg >>:‘).strip() if len(msg) == 0: continue #检查msg的信息,防止无输入信息 #client.send(b"Hello, world!") #发送信息 client.send(msg.encode(‘utf-8‘)) data = client.recv(1024) #默认接受1024字节,就是1k # with open(‘filename‘, ‘w‘) as ret: # 这两行就 可以用过来传输文件了。 # ret = data.write() print(data.decode()) client.close() #关闭端口
原文地址:https://www.cnblogs.com/Ian-learning/p/8454597.html
时间: 2024-10-28 19:56:14