Python用GET方法上傳檔案_python

來源:互聯網
上載者:User

之前在osc看到一個文章討論Get和Post的不同, 有人說不能用Get來上傳檔案。這就是用Get上傳檔案的例子,client用來發Get請求,server用來收請求。檔案內容是在http請求的body內傳過去的。用了不同的語言,因為我覺得各自處理起來都要方便些。而且我覺得瀏覽器也是可以發出這樣的請求的,之後我會嘗試一下。

請求端代碼

複製代碼 代碼如下:

import requests #需要安裝requests
with open('test.txt', 'rb') as f:
    requests.get('http://127.0.0.1:9999', data=f)

服務端代碼

複製代碼 代碼如下:

var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res){
    //console.log(req);
    var recData = "";
    req.on('data', function(data){
        recData += data;
    })
    req.on('end', function(data){
        recData += data;
        fs.writeFile('recData.txt', recData, function(err){
            console.log('file received');
        })
    })
    res.end('hello');
})
server.listen(9999);

以上就是本文的所有代碼了,希望對大家理解get方法上傳檔案能夠有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.