日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python flask 大文件 下载_flask - python上传大文件到服务器报错

發布時間:2023/12/19 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python flask 大文件 下载_flask - python上传大文件到服务器报错 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問 題

項目需要,在mac上將打包好的文件(四五百兆)自動上傳到web后臺,用了兩種方式上傳,都報了類似的錯誤,在windows和linux上測試不會報錯,但是到了mac打包機上就會報錯:

①第一種報錯:

Traceback (most recent call last):

File "my_upload.py", line 61, in

upload_pack(f)

File "my_upload.py", line 54, in upload_pack

r = requests.post(url, form, files=files)

File "/Library/Python/2.7/site-packages/requests-2.9.1-py2.7.egg/requests/api.py", line 107, in post

return request('post', url, data=data, json=json, **kwargs)

File "/Library/Python/2.7/site-packages/requests-2.9.1-py2.7.egg/requests/api.py", line 53, in request

return session.request(method=method, url=url, **kwargs)

File "/Library/Python/2.7/site-packages/requests-2.9.1-py2.7.egg/requests/sessions.py", line 468, in request

resp = self.send(prep, **send_kwargs)

File "/Library/Python/2.7/site-packages/requests-2.9.1-py2.7.egg/requests/sessions.py", line 576, in send

r = adapter.send(request, **kwargs)

File "/Library/Python/2.7/site-packages/requests-2.9.1-py2.7.egg/requests/adapters.py", line 426, in send

raise ConnectionError(err, request=request)

requests.exceptions.ConnectionError: ('Connection aborted.', error(32, 'Broken pipe'))

代碼:

# -*- coding: UTF-8 -*-

import requests

import json

import sys

reload(sys)

sys.setdefaultencoding("utf-8")

url = 'http://10.242.12.121:9096/auto_upload/'

# config

channel = unicode('test2').encode('utf-8')

form = {'platform': 'Android', 'channel': channel, 'publish_time': '2016-08-01 00:00', 'remarks': 'auto_test', 'code': 'HH'}

files = {'file': ('D3174449.apk', open('D3174449.apk', 'rb'), 'application/octet-stream')}

print 'upload...'

s = requests.session()

r = s.post(url, form, files=files, timeout=500)

print r.text

# print unicode(json.loads(r.text)).encode('utf-8')

print 'upload_end'

第二種報錯:urllib2.URLError:

代碼:

#!/usr/bin/python

# -*- coding: utf-8 -*-

import sys

reload(sys)

sys.setdefaultencoding("utf-8")

# buld post body data

import urllib2

import time

boundary = '----WebKitFormBoundary3hYfNZFUuz8yiMid'

data = []

data.append('--%s' % boundary)

data.append('Content-Disposition: form-data; name="%s"\r\n' % 'platform')

data.append('Android')

data.append('--%s' % boundary)

data.append('Content-Disposition: form-data; name="%s"\r\n' % 'channel')

data.append('360')

data.append('--%s' % boundary)

data.append('Content-Disposition: form-data; name="%s"\r\n' % 'publish_time')

data.append('2016-08-23 00:00')

data.append('--%s' % boundary)

data.append('Content-Disposition: form-data; name="%s"\r\n' % 'remarks')

data.append('hh2')

data.append('--%s' % boundary)

data.append('Content-Disposition: form-data; name="%s"\r\n' % 'code')

data.append('HH')

data.append('--%s' % boundary)

fr = open(u'./D3174449.apk', 'rb')

data.append('Content-Disposition: form-data; name="%s"; filename="D3174449.apk"' % 'file')

data.append('Content-Type: %s\r\n' % 'application/octet-stream')

data.append(fr.read())

fr.close()

data.append('--%s--\r\n' % boundary)

http_url = 'http://10.242.12.121:9096/auto_upload/'

http_body = '\r\n'.join(data)

print 'upload...'

try:

# buld http request

req = urllib2.Request(http_url, data=http_body)

# header

#req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)

#req.add_header('User-Agent', 'Mozilla/5.0')

#req.add_header('Referer', 'http://10.246.13.129:9099/auto_upload/')

req.add_header('Accept', 'application/json, text/javascript, */*; q=0.01')

req.add_header('Accept-Encoding', 'gzip, deflate')

req.add_header('Accept-Language', 'zh-CN,zh;q=0.8')

req.add_header('Connection', 'keep-alive')

#req.add_header('Content-Length', '620')

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

req.add_header('Cookie', 'session=.eJyrVkosLclQsjLUUUrOT0lVsqpWUkhSslIKdgmsUqoFCRYVALkZGXkmBhYGSjpKqbmJmTkIEQeQAr281JLUxOJUveT8XKCStNKcnLzEXKBhSjGlFgaphjGlZilmlkq1AP-KIUg.Cp7ldw.cDtv8It9wXz12qb7JbI5gf08rew')

req.add_header('Origin', 'http://10.246.13.129:9096')

req.add_header('Referer', 'http://10.246.13.129:9096/auto_upload/')

req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36')

req.add_header('X-Requested-With', 'XMLHttpRequest')

# post data to server

resp = urllib2.urlopen(req, timeout=500)

# get response

qrcont = resp.read()

print qrcont

except Exception, e:

print e

print 'upload end'

搜索能力有限,去谷歌搜了下沒有找到合適的解決辦法,或者是理解不夠:

https://github.com/kennethrei...

服務端代碼:

def auto_upload():

"""

上傳文件

"""

try:

file = request.files['file']

except:

print 'file fail'

print 'myautofile_debug', file

platform = request.form.get('platform')

channel = request.form.get('channel')

publish_time = request.form.get('publish_time')

remarks = request.form.get('remarks')

code = request.form.get('code')

#create channel if channel does not exist

data = {'platform': platform, 'channel': channel, 'remarks': '', 'app_num': 0, 'code': code}

result = app.config['mongo'].insert_channel_data(data)

print 'result:', result

#if result is not False:

# 增加渠道目錄

platform_dir = os.path.join(app.config['UPLOAD_FOLDER'], code, platform)

channel_dir = os.path.join(platform_dir, channel)

print 'add_channel', platform_dir, channel_dir, os.path.exists(platform_dir), os.path.exists(channel_dir)

#try:

if not os.path.exists(platform_dir):

os.mkdir(platform_dir)

if not os.path.exists(channel_dir):

os.mkdir(channel_dir)

#except:

#return Response(json.dumps('fail'), mimetype='application/json')

# channel = channel.encode('utf-8')

relative_path = code + os.sep + platform + os.sep + channel + os.sep

#print 'upload_file', relative_path

if file:

# filename = secure_filename(file.filename)

filename = file.filename

filename = gen_file_name(filename, relative_path)

mimetype = file.content_type

#print 'auto_mimetype:', mimetype

if not allowed_file(file.filename, platform):

#print 'not allowed'

result = uploadfile(name=filename, type=mimetype, size=0, not_allowed_msg="文件類型錯誤!")

else:

# save file to disk

uploaded_file_path = os.path.join(app.config['UPLOAD_FOLDER'], relative_path + filename)

file.save(uploaded_file_path)

# get file size after saving

size = os.path.getsize(uploaded_file_path)

size_str = _formatFileSize(size)

#print 'size', size, size_str

# save data into database

data = {}

data['code'] = code

data['platform'] = platform

data['channel'] = channel

data['filename'] = filename

data['url'] = uploaded_file_path

data['size'] = size_str

data['publish_time'] = publish_time

data['upload_time'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

data['remarks'] = remarks

app.config['mongo'].insert_app_data(data)

# return json for js call back

result = uploadfile(name=filename, type=mimetype, size=size, relative_path = relative_path)

return json.dumps({"files": [result.get_file()]})

解決方案

你這個一般都需要結合服務器端的軟件代碼、框架版本之類的來解決啊,不過既然你特意說了Windows、Linux和MAC的差異,我只知道一個比較重要的差異,就是MAC操作系統在對方shutdown的情況下不能保持半連接狀態,一段時間后悔自動關閉,所以如果你的服務器端在還沒有傳輸完文件的情況下就以某種方式SHUTDOWN了寫的一端,就會在傳輸中出錯,比如說先發送了返回值,再慢慢接收數據。沒有別的信息的話只能先這么猜了。

掃一掃關注IT屋

微信公眾號搜索 “ IT屋 ” ,選擇關注與百萬開發者在一起

總結

以上是生活随笔為你收集整理的python flask 大文件 下载_flask - python上传大文件到服务器报错的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。