python socket通信編程實(shí)現(xiàn)文件上傳代碼實(shí)例
這篇文章主要介紹了python socket通信編程實(shí)現(xiàn)文件上傳代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
寫(xiě)一個(gè)file_receive.py和一個(gè)file_send.py程序,由file_send.py上傳一個(gè)文件,file_receive.py接收上傳的文件,寫(xiě)到指定的包內(nèi)
#file_receive.py
import socket,subprocess,os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sk = socket.socket()
address = ('127.0.0.1',8001)
sk.bind(address)
sk.listen(3)
conn,addr = sk.accept()
fileinfo = conn.recv(1024)
filename,filesize = str(fileinfo,'utf8').split('|')
#filename = str(filename,'utf8')
#filesize = int(str(filesize,'utf8'))
path = os.path.join(BASE_DIR,'file_recv',filename)
f = open(path,'wb')
has_received = 0
while has_received != int(filesize):
data = conn.recv(1024)
f.write(data)
has_received += len(data)
f.close()
print('well done')
sk.close()
#file_send.py
import socket,os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sk = socket.socket()
address = ('127.0.0.1',8001)
sk.connect(address)
filename = input("please input filename:")
path = os.path.join(BASE_DIR,filename)
filesize = os.stat(path).st_size
fileinfo = '%s|%s'%(filename,str(filesize))
sk.sendall(bytes(fileinfo,'utf8'))
f = open(path,'rb')
has_sent = 0
while has_sent != int(filesize):
data = f.read(1024)
sk.sendall(data)
has_sent += len(data)
print('well done!')
f.close()
sk.close()
文件運(yùn)行后,實(shí)現(xiàn)了將file_send.py上傳的test.png文件上傳到當(dāng)前路徑下的file_recv包內(nèi).
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python SOCKET編程基礎(chǔ)入門(mén)
- python Socket網(wǎng)絡(luò)編程實(shí)現(xiàn)C/S模式和P2P
- python網(wǎng)絡(luò)編程:socketserver的基本使用方法實(shí)例分析
- python網(wǎng)絡(luò)編程socket實(shí)現(xiàn)服務(wù)端、客戶端操作詳解
- 基于python3的socket聊天編程
- Python 網(wǎng)絡(luò)編程之TCP客戶端/服務(wù)端功能示例【基于socket套接字】
- Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
- python粘包問(wèn)題及socket套接字編程詳解
- Python Socket編程詳解
相關(guān)文章
Pycharm debug調(diào)試時(shí)帶參數(shù)過(guò)程解析
這篇文章主要介紹了Pycharm debug調(diào)試時(shí)帶參數(shù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Python并發(fā)之多進(jìn)程的方法實(shí)例代碼
這篇文章主要介紹了Python并發(fā)之多進(jìn)程的方法實(shí)例代碼,文中也提到了進(jìn)程與線程的共同點(diǎn),需要的朋友跟隨腳本之家小編一起看看吧2018-08-08
python實(shí)現(xiàn)進(jìn)度條和系統(tǒng)通知的示例詳解
這篇文章主要和大家分享兩個(gè)有意思的Python小工具,可以優(yōu)雅地實(shí)現(xiàn)進(jìn)度條和系統(tǒng)通知,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴快也跟隨小編一起學(xué)習(xí)一下2023-11-11
Python時(shí)間管理黑科技之datetime函數(shù)詳解
在Python中,datetime模塊是處理日期和時(shí)間的標(biāo)準(zhǔn)庫(kù),它提供了一系列功能強(qiáng)大的函數(shù)和類(lèi),用于處理日期、時(shí)間、時(shí)間間隔等,本文將深入探討datetime模塊的使用方法,感興趣的可以了解下2023-08-08
Python獲取系統(tǒng)默認(rèn)字符編碼的方法
這篇文章主要介紹了Python獲取系統(tǒng)默認(rèn)字符編碼的方法,涉及Python中sys模塊getdefaultencoding方法的使用技巧,需要的朋友可以參考下2015-06-06
10個(gè)python爬蟲(chóng)入門(mén)基礎(chǔ)代碼實(shí)例 + 1個(gè)簡(jiǎn)單的python爬蟲(chóng)完整實(shí)例
這篇文章主要介紹了10個(gè)python爬蟲(chóng)入門(mén)基礎(chǔ)代碼實(shí)例和1個(gè)簡(jiǎn)單的python爬蟲(chóng)爬蟲(chóng)貼吧圖片的實(shí)例,需要的朋友可以參考下2020-12-12
Python 轉(zhuǎn)換文本編碼實(shí)現(xiàn)解析
這篇文章主要介紹了Python 轉(zhuǎn)換文本編碼實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值2019-08-08

