Home > Backend Development > Python Tutorial > Python用GET方法上传文件

Python用GET方法上传文件

WBOY
Release: 2016-06-10 15:17:29
Original
1222 people have browsed it

之前在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方法上传文件能够有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template