pythonsocket傳輸文件
發布時間: 2022-04-17 10:37:10
A. 如何通過python實現簡單socket通信
Python中實現socket通信,socket通信的服務端比較復雜,而客戶端非常簡單,所以客戶端基本上都是用sockct模塊實現,而服務端用有很多模塊可以使用。
B. 怎麼使用python socket以流式形式一行行發送csv文件中的數據
Python種解釋型、面向象、態數據類型高級程序設計語言 sql 結構化查詢語言 sql相容易
C. python中,如何將一個指定的文件發送到對方主機的指定埠呢(用socket方式) 新手求指教!
#發送端
#-*-coding:utf-8-*-
importTkinter
importtkFileDialog
fromsocketimport*
importstruct
importos
defclient(filename):
Addr=('127.0.0.1',8000)
BUFSIZE=2048
Sendsocket=socket(AF_INET,SOCK_STREAM)
Sendsocket.connect(Addr)
FileHead=struct.pack('128s11i',filename,0,0,0,0,0,0,0,0,os.stat(filename).st_size,0,0)
Sendsocket.send(FileHead)
fp=open(filename,'rb')
while1:
FileData=fp.read(BUFSIZE)
ifnotFileData:
break
Sendsocket.send(FileData)
fp.close()
Sendsocket.close()
defFileOpen():
r=tkFileDialog.askopenfilename(title='PythonTkinter',
filetypes=[('Allfiles','*')])
filename=r.split('/')
client(filename[-1])
root=Tkinter.Tk()
button1=Tkinter.Button(root,text='FileOpen',height=8,width=14,bg='blue',
command=FileOpen)
button1.pack()
root.mainloop()#接收端
#-*-coding:utf-8-*-
fromsocketimport*
importos
importstruct
Addr=('127.0.0.1',8000)
BUFSIZE=1024
FileInfoSize=struct.calcsize('128s32sI8s')
SocketRecv=socket(AF_INET,SOCK_STREAM)
SocketRecv.bind(Addr)
SocketRecv.listen(5)
print"waitting........"
conn,addr=SocketRecv.accept()
print"sendfrom",addrFileHead=conn.recv(FileInfoSize)
filename,temp1,filesize,temp2=struct.unpack('128s32sI8s',FileHead)
printfilename,len(filename),type(filename)
printfilesize
filename='new_'+filename.strip('