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

pythonhttp文件上傳

發布時間: 2022-09-09 19:08:59

『壹』 如何在 python 中模擬 post 表單來上傳文件

額,這個其實查文檔就可以實現,唯一有困難的地方在於你用的python版本,python2和3的實現庫不太一樣

python2里

importurllib2,urllib

data={'name':'www','password':'123456'}
f=urllib2.urlopen(url='http://www.xxx.net/',data=urllib.urlencode(data))

python3里

importurllib

url="http://www.xxx.net"
postdata=urllib.parse.urlencode({'name':'www','password':'123456'})
res=urllib.request.urlopen(url,postdata)

改了一次代碼排亂了,你自己湊活看吧

『貳』 python文件轉位元組流上傳

圖片,視頻都是二進制的,你讀取的時候寫 rb ,然後你在請求的時候記得把視頻轉成.content

像這樣load_src = requests.get("http://"+srcs[i], headers=headers).content

『叄』 python mac 瀏覽器傳文件

selenium提供的sendkeys方法上傳文件我就不贅述,網上很多教程,但是缺點也很明顯並不是完全適用各種場景如果上傳文件不是input類型,就完全無法適用就算是input也不能完全適用亦沒辦法完全模擬用戶的操作。
在執行當前輸入路徑地址的時候,必須切換為英文輸入法,否則會導致輸入地址後無法跳轉需要注意在mac上必須打開安全性與隱私中的輔助功能完全控制。
Python由荷蘭數學和計算機科學研究學會的吉多·范羅蘇姆於1990年代初設計,作為一門叫做ABC語言的替代品Python提供了高效的高級數據結構,還能簡單有效地面向對象編程

『肆』 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代碼上傳到伺服器上

使用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的文件上傳

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,急著用圖片上傳,請問誰知道怎麼寫上傳()

你得有伺服器端的介面才能給你具體的,不過你先湊活著看這個例子吧:

#---------upload_file.py----------------
#
c=pycurl.Curl()
c.setopt(c.POST,1)
c.setopt(c.URL,"http://127.0.0.1:8000/receive/")
c.setopt(c.HTTPPOST,[("file1",(c.FORM_FILE,"c:\tmp\download\test.jpg"))])
#c.setopt(c.VERBOSE,1)
c.perform()
c.close()
print"that'sit;)"

去這里下載pycurl:https://github.com/pycurl/pycurl

『捌』 怎麼用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:

熱點內容
安卓轉iphone有什麼不好的 發布:2024-10-11 18:13:22 瀏覽:584
同步兩個文件夾 發布:2024-10-11 17:49:02 瀏覽:481
SQL赴日 發布:2024-10-11 17:28:25 瀏覽:389
新建文件夾的英語 發布:2024-10-11 17:28:24 瀏覽:447
對稱加密應用 發布:2024-10-11 17:27:48 瀏覽:441
ftp帶字幕 發布:2024-10-11 17:19:37 瀏覽:431
android測試環境 發布:2024-10-11 17:10:45 瀏覽:798
如何查找內網伺服器地址 發布:2024-10-11 16:54:55 瀏覽:395
cvs編譯器哪個好 發布:2024-10-11 16:54:51 瀏覽:677
SQL資料庫文件的類型 發布:2024-10-11 16:35:12 瀏覽:115