socket编程-客户端向服务器端发送某目录下的所有文件名
服务器端:
import socket if __name__ == '__main__': server = socket.socket() server.bind(('127.0.0.1',9999)) server.listen() s1, raddr = server.accept() while True: data = s1.recv(1024) strdata = data.decode() if strdata == 'end': break print(strdata) #mystr = '服务器端' #res = mystr.encode() s1.send(b'ok') s1.close() server.close()
客户端:
import socket import sys import os if __name__ == '__main__': namelist = os.listdir('C:/Users/96294/Desktop/python3/bishelianxi/clientSendingPictureToServerAndFeedback') #mystr = '客户端' #res = mystr.encode() address = ('127.0.0.1',9999) sock = socket.socket() sock.connect(address) for item in namelist: sock.send(item.encode()) data = sock.recv(1024) if data.decode() == 'ok': print('successfully send') sock.send(b'end') sock.close()