當前位置:首頁 » 文件管理 » python上傳文件

python上傳文件

發布時間: 2022-01-29 03:32:40

㈠ 如何通過python上傳文件到百度雲盤

直接在系統命令行中輸入bypy命令,將會列出所有的命令的使用信息。
授權
在命令行中輸入bypy info,將會出現一個提示,按照提示完成授權,完成了授權Python代碼才能和你的網路雲盤進行通信。
常用命令
新建文件夾,在網路網盤中新建一個文件夾:
mkdir(remotepath='bypy'),將會新建一個bypy文件夾,如圖:
新建的文件夾
上傳文件:
upload(localpath='c:\\new\\timg.jpg',remotepath='bypy',onp='new')
參數說明:
localpath:本地的目錄,如果省略則為當前目錄。
remotepath:雲盤目錄
onp:當出現重復文件時如何處理,默認是overwrite,安全起見可以更改為new
Python 代碼實例
from bypy import ByPy
bp = ByPy()
bp.mkdir(remotepath='bypy')
bp.upload(localpath='c:\\new\\timg.jpg',remotepath='bypy',onp='new')
print('上傳完畢!')
注意:
中文文件名可能會出現問題,最好使用英文文件名。

㈡ 如何用 Python 做大文件上傳的服務端

這個果斷要用tornado啊。html5的 Filesystem Api,可以讀取一個本地文件為blob,然後可以按任意位元組切分slice。這不就是斷點上傳么。

Google Gears時代我就實現了個多線程上傳的。python的socket手寫http協議。很好玩。

㈢ 怎麼用http上傳一個文件到伺服器 python

首先,標准HTTP協議對上傳文件等表單的定義在這里:wwwietforg/rfc/rfc1867txt 大概數據包格式如下:

單文件:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="field1"

Joe Blow
--AaB03x
content-disposition: form-data; name="pics"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--
多文件:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="field1"

Joe Blow
--AaB03x
content-disposition: form-data; name="pics"
Content-type: multipart/mixed, boundary=BbC04y

--BbC04y
Content-disposition: attachment; filename="file1.txt"
其次,python上傳文件的幾種方法:

1 自己封裝HTTP的POST數據包:http//stackoverflowcom/questions/680305/using-multipartposthandler-to-post-form-data-with-python

import httplibimport mimetypesdef post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = httplib.HTTP(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() return h.file.read() def encode_multipart_formdata(fields, files): LIMIT = '----------lImIt_of_THE_fIle_eW_$' CRLF = '\r\n' L = [] for (key, value) in fields: L.append('--' + LIMIT) L.append('Content-Disposition: form-data; name="%s"' % key) L.append('') L.append(value) for (key, filename, value) in files:

㈣ Python的文件上傳

Python中使用GET方法實現上傳文件,下面就是用Get上傳文件的例子,client用來發Get請求,server用來收請求。

請求端代碼:

importrequests#需要安裝requests
withopen('test.txt','rb')asf:
requests.get('http://伺服器IP地址:埠',data=f)

服務端代碼:

varhttp=require('http');
varfs=require('fs');
varserver=http.createServer(function(req,res){
//console.log(req);
varrecData="";
req.on('data',function(data){
recData+=data;
})
req.on('end',function(data){
recData+=data;
fs.writeFile('recData.txt',recData,function(err){
console.log('filereceived');
})
})
res.end('hello');
})
server.listen(埠);

㈤ 如何在 Python 中模擬 post 表單來上傳文件

最近有個將文件上傳到內部web伺服器上的任務,於是參考了網上部分源碼用python寫了這個小程序,代碼如下:

[python]view plain

  • #coding:utf-8

  • '''''

  • Createdon2015.3.19

  • @author:damofy

  • '''

  • importos

  • importtime

  • importsys

  • importurllib2

  • '''''

  • filename待上傳的文件

  • fieldname表單域中的name屬性

  • '''

  • defCreateBody(filename,fieldname,strBoundary):

  • bRet=False

  • sData=[]

  • sData.append('--%s'%strBoundary)

  • #'Content-Disposition:form-data;name="uploadfile";filename="XX-Net-1.3.6.zip"'

  • sData.append('Content-Disposition:form-data;name="%s";'%fieldname+'filename="%s"'%os.path.basename(filename))

  • sData.append('Content-Type:%s '%'application/octet-stream')

  • try:

  • pFile=open(filename,'rb')

  • sData.append(pFile.read())

  • sData.append('--%s-- '%strBoundary)

  • bRet=True

  • finally:

  • pFile.close()

  • returnbRet,sData

  • defuploadfile(http_url,filename,fieldname):

  • ifos.path.exists(filename):

  • filesize=os.path.getsize(filename)

  • print('file:'+filename+'is%dbytes!'%filesize)

  • else:

  • print('file:'+filename+'isn'texists!')

  • returnFalse

  • strBoundary='---------------------------%s'%hex(int(time.time()*1000))

  • bRet,sBodyData=CreateBody(filename,fieldname,strBoundary)

  • ifTrue==bRet:

  • http_body=' '.join(sBodyData)

  • stReq=urllib2.Request(http_url,http_body)

  • stReq.add_header('User-Agent','Mozilla/5.0')

  • stReq.add_header('Content-Length:','%d'%filesize)

  • stReq.add_header('Content-Type','multipart/form-data;boundary=%s'%strBoundary)

  • resp=urllib2.urlopen(stReq,timeout=5)

  • #getresponse

  • msg=resp.read()

  • print("Responsecontent: "+msg)

  • else:

  • print("CreateBodyfailed!")

  • returnbRet

  • if__name__=='__main__':

  • iflen(sys.argv)>2:

  • http_url=sys.argv[1]

  • filename=sys.argv[2]

  • else:

  • print('pythonupload.pyhttp://10.20.131.23/upload./test.dat')

  • sys.exit()

  • #參數3"uploadfile"是post表單中的name屬性,需要與服務端保持一致

  • uploadfile(http_url,filename,'uploadfile')



㈥ python、FileReference、上傳文件

數據微化處理傳送

㈦ 如何把python代碼上傳到伺服器上

使用pip或easy_install可以管理和安裝python的package包,實際上它們都是從pypi伺服器中搜索和下載package的。目前在pypi伺服器上,有超過三萬多個package,同時還允許我們將自己的代碼也上傳發布到伺服器上。這樣,世界上的所有人都能使用pip或easy_install來下載使用我們的代碼了。
具體步驟如下:
首先創建項目文件和setup文件。
目錄文件結構如下:
project/
simpletest/
__init__.py
test.py
setup.py
假設項目文件只有一個simpletest包,裡面有一個test.py文件。
創建的setup.py文件格式大致如下,其中,install_requires欄位可以列出依賴的包信息,用戶使用pip或easy_install安裝時會自動下載依賴的包。詳細的格式參考文檔。
from setuptools import setup, find_packages
setup(
name = 'simpletest',
version = '0.0.1',
keywords = ('simple', 'test'),
description = 'just a simple test',
license = 'MIT License',
install_requires = ['simplejson>=1.1'],
author = 'yjx',
author_email = '[email protected]',

packages = find_packages(),
platforms = 'any',
)
然後將代碼打包。
打包只需要執行python
setup.py xxx命令即可,其中xxx是打包格式的選項,如下:
# 以下所有生成文件將在當前路徑下 dist 目錄中
python setup.py bdist_egg # 生成easy_install支持的格式
python setup.py sdist # 生成pip支持的格式,下文以此為例
發布到pypi。
發布到pypi首先需要注冊一個賬號,然後進行如下兩步:
注冊package。輸入python setup.py register。
上傳文件。輸入python setup.py sdist upload。
安裝測試
上傳成功後,就可以使用pip來下載安裝了。
另外,pypi還有一個測試伺服器,可以在這個測試伺服器上做測試,測試的時候需要給命令指定額外的"-r"或"-i"選項,如python
setup.py register -r "",python
setup.py sdist upload -r "",pip
install -i "" simpletest。
發布到測試伺服器的時候,建議在linux或cygwin中發布,如果是在windows中,參考文檔,需要生成.pypirc文件

㈧ python能直接通過url上傳文件嗎

可以這樣寫:
urls = sel.xpath('//li[@class="next_article"]/a/@href').extract()
for url in urls:
url = "http://blog.csdn.net" + url
yield Request(url, callback=self.parse)

㈨ python selenium怎麼自動上傳文件

給你個例子:

ifnotimg_path:
img_path=os.path.abspath("../res/cvd_test.jpg")
else:
img_path=path.abspath(img_path)
#
up_img_input=WebDriverWait(self.driver,10).until(
EC.presence_of_element_located((By.ID,"updli_file"))
)
up_img_input.send_keys(img_path)
send_img_btn=WebDriverWait(self.driver,10).until(
EC.element_to_be_clickable((By.ID,"send-pic-img"))
)
#time.sleep(self.send_interval)
send_img_btn.click()

上面實現的工作就是,找到上傳組件的那個input元素,然後把你要上傳的文件的路徑發送給ta

㈩ pythonweb前後端分離項目怎麼實現單文件上傳

在HTML上傳到後台接收時,又要把接收到的文件或圖片復制到項目下面,要不然就沒顯示。所以自己總結了下怎麼樣把文件上傳到指定文件目錄下
感覺還挺實用的,別人通過區域網訪問我的項目來上傳圖片,我的項目也能接收復制到指定目錄下,伺服器沒試過,但還是來分享下。

熱點內容
117小游戲伺服器ip 發布:2024-04-17 06:11:37 瀏覽:446
奧迪什麼配置好看 發布:2024-04-17 06:02:28 瀏覽:764
算命網源碼 發布:2024-04-17 05:53:16 瀏覽:295
C語言數組的遍歷 發布:2024-04-17 05:45:18 瀏覽:660
劃分清演算法 發布:2024-04-17 05:44:41 瀏覽:667
bitlocker暫停加密 發布:2024-04-17 05:25:21 瀏覽:542
腳本怎麼刷 發布:2024-04-17 05:09:50 瀏覽:134
如何調取手機存儲 發布:2024-04-17 05:06:50 瀏覽:920
linux的home 發布:2024-04-17 05:02:47 瀏覽:378
安卓王者榮耀怎麼快速退款 發布:2024-04-17 05:01:46 瀏覽:850