Python写的一个文件上传upload

#!/usr/bin/env python
#coding:utf-8
'''
@date:2014-11-30
@filename:simpleUp.py
'''
import web


urls=(
   '/','index',
   '/test','mytest',
   '/upload','Upload',
)


app = web.application(urls,locals())


class mytest:
def GET(self):
return "<b><h1>mytest</h1></b>"
class Upload:
def GET(self):
return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="">
<input type="file" name="myfile"/>
<br/>
<input type="submit"/>
</form>
</body></html>"""
def POST(self):
x = web.input(myfile={})
web.debug(x['myfile'].filename)
web.debug(x['myfile'].value)
web.debug(x['myfile'].file.read())
print x['myfile'].filename
print x['myfile'].value
print x['myfile'].file.read()
raise web.seeother('/upload')
if __name__=='__main__':

app.run()
好的,以下是Python上传文件到蓝奏云的示例代码: ```python import requests def upload_file_to_lanzouyun(file_path, folder_id, cookie): # 设置请求头 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", "Cookie": cookie } # 获取上传地址和token url = "https://pc.woozooo.com/fileup.php" params = { "folder_id": folder_id } resp = requests.get(url, params=params, headers=headers) if resp.status_code != 200: raise Exception("获取上传地址和token失败") data = resp.json() if data["zt"] != 1: raise Exception(data["info"]) # 上传文件 url = data["url"] token = data["token"] data = { "task": "1", "folder_id": folder_id, "id": data["id"], "upload_file": (file_path, open(file_path, "rb"), "application/octet-stream") } resp = requests.post(url, data=data, headers=headers, params={"task": "1", "token": token}) if resp.status_code != 200: raise Exception("上传文件失败") data = resp.json() if data["zt"] != 1: raise Exception(data["info"]) # 返回文件信息 return data["text"] ``` 其中,`file_path`是要上传的文件的路径,`folder_id`是上传到的文件夹的ID,`cookie`是登录蓝奏云后的Cookie。 使用方法: ```python file_path = "example.txt" folder_id = "1234567890" # 文件夹ID需要自行获取 cookie = "这里填你的Cookie" result = upload_file_to_lanzouyun(file_path, folder_id, cookie) print(result) ``` 需要注意的是,这个示例代码仅供参考,实际使用时可能需要根据情况进行修改和调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值