DAY18-作业

QQ图片20190814164436.jpg
//server
import socket,os

server = socket.socket(family=-1, type=-1)
server.bind(('10.7.181.70', 8888))
server.listen(512)

while True:
   connect, address = server.accept()
   recv_data = connect.recv(102400)
   if recv_data.decode() == 'a':
       inp = '假装这是服务器发送的文字信息'
       connect.send(inp.encode())
   elif recv_data.decode() == 'b':
       connect.send(str(os.listdir('file')).encode())
       recv_data = connect.recv(102400)
       for name in os.listdir('file'):
           if recv_data.decode() == name:
               connect.send('有该图片'.encode())
               with open('./file/'+name, 'rb') as f:
                   connect.send(f.read())
               break
       else:
           connect.send('木有该图片'.encode())
//client
import socket

while True:
   client = socket.socket()
   client.connect(('10.7.181.70', 8888))
   inp = input('a.请求文字信息\nb.请求图片信息\n请选择:')
   client.send(inp.encode())
   re_data = client.recv(102400)
   print('接收到:', re_data.decode(encoding='utf-8'))
   if inp == 'b':
       inp = input('请输入查看的图片名:')
       client.send(inp.encode())
       re_data = client.recv(102400)
       print(re_data.decode(encoding='utf-8'))
       if re_data.decode(encoding='utf-8') == '有该图片':
           re_data = client.recv(102400)
           with open(inp, 'wb') as f:
               f.write(re_data)
           print('加载完成')

你可能感兴趣的:(DAY18-作业)