python flask 上传大文件如1G以上 413错误解决
2022-2-12 python
生产环境:flask waitress
在flask项目中,上传超过1GB文件时后台报http 413 Request Entity Too Large 请求体太大错误!
解决方法:
1. 修改adjustments.py的max_request_body_size
如设置max_request_body_size=1073741824*5,则小于5G的文件都能上传之后到MAX_CONTENT_LENGTH再判断
2.在flask配置中设置 MAX_CONTENT_LENGTH的值;
如设置为2G ( MAX_CONTENT_LENGTH=2*1024*1024*1024) 这时小于20M的文件都可以上传
标签: python flask large file upload
python scrapy爬取XX基金
2020-12-23
需求:爬取XX基金的所有基金信息写入mongodb,同步到mysql并界面显示
利用dll生成lib
2018-4-29 C++
业务场景:
使用libvlc3.02 SDK提供的lib及dll编译运行程序,提示找不到XXX于动态链接库libvlc.dll上
解决办法:
直接利用libvlc.dll生成libvlc.lib
步骤:
dumpbin.exe:dll导出函数
C:\Program Files (x86)\Microsoft Visual Studio 8\VC\bin>dumpbin libvlc.dll /EXPORTS /OUT:libvlc.def
编辑libvlc.def只留下函数所在的行
1 0 00002B30 libvlc_add_intf
2 1 0000BB60 libvlc_audio_equalizer_get_amp_at_index
3 2 0000B980 libvlc_audio_equalizer_get_band_count
python:格式化def
file = open(r"D:\Beihai\python\libvlco.def")
files = open(r"D:\Beihai\python\libvlc.def", "w")
fc = 1
while 1 :
line = file.readline()
if not line :
break
sl = line.split()
#print sl[-1]
nl = sl[-1] + " @ " + str(fc) + "\n"
fc = fc + 1
files.write(nl)
file.close()
files.close()
lib:利用def生成lib
PHP实现soap null wsdl服务端,android 实现android client访问
2017-3-11 android
利用php实现soap的服务端供app等客户端访问
服务端:
新建php文件testServer.php
<?php
class service
{
public function HelloWorld()
{
return "hello";
}
}
$s = new SoapServer(null, array('uri' => 'test'));
$s->setClass("service");
$s->handle();
?>
客户端:
PHP调用:
import urllib2
SM_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
</soap:Header>
<soap:Body>
<HelloWorld>
</HelloWorld>
</soap:Body>
</soap:Envelope>"""
url = "http://www.qs77.net/testServer.php"
http_headers = {
"Accept": "application/soap+xml,multipart/related,text/*",
"Cache-Control":"no-cache",
"Content-Type": "text/xml; charset=utf-8"
}
request = urllib2.Request(url, SM_TEMPLATE, http_headers)
response = urllib2.urlopen(request)
print response.read()
android调用:
new Thread(new Runnable() {
@Override public void run() {
try {
final String soaptemp = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" + " <soap:Header>" + " </soap:Header>" + " <soap:Body>" + " <HelloWorld>" + " </HelloWorld>" + " </soap:Body>" + "</soap:Envelope>";
URL url = new URL("http://www.qs77.net/testService.php"); HttpURLConnection urlCon = (HttpURLConnection) url.openConnection(); urlCon.setUseCaches(false); urlCon.setRequestProperty("Accept", "application/soap+xml,multipart/related,text/*"); urlCon.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); urlCon.setRequestProperty("Content-Length", "" + soaptemp.getBytes().length); urlCon.setRequestProperty("soapAction", "HelloWorld"); urlCon.setRequestMethod("POST"); urlCon.setDoOutput(true); urlCon.setDoInput(true); OutputStream out = urlCon.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(soaptemp); writer.flush(); writer.close();
int status = urlCon.getResponseCode(); WolfLog.e(TAG, "" + status); InputStream in = null; if (status == 200) { in = urlCon.getInputStream(); } else { in = urlCon.getErrorStream(); } BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); String inputLine; StringBuilder input = new StringBuilder();
while ((inputLine = reader.readLine()) != null) { input.append(inputLine); } Document document = DocumentHelper.parseText(input.toString()); Element elementRoot = document.getRootElement(); Iterator<Element> iter = elementRoot.elementIterator();
while (iter.hasNext()) {
Element element = (Element)iter.next();
if ("Body".equals(element.getName())) { Iterator<Element> innerIter = element.elementIterator();
while (innerIter.hasNext()) { Element innerElement = (Element)innerIter.next(); WolfLog.e(TAG, innerElement.getName());
if ("HelloWorldResponse".equals(innerElement.getName())) { Iterator<Element> iinnerIter = innerElement.elementIterator();
while(iinnerIter.hasNext()) {
Element iinnerElement = (Element) iinnerIter.next();
if ("return".equals(iinnerElement.getName())) { Message msg = mHandler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putString("msg", iinnerElement.getText()); msg.setData(bundle);
mHandler.sendMessage(msg);
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
标签: android python soap php dom4j
使用python改变文件夹中的代码文件编码
2017-1-18 python
现状:
项目以前在windows底下写的使用的编码是gbk
在mac os底下用xcode显示中文乱码
解决方式:
把项目底下文件的编码改为utf-8编码
使用工具:
python
代码如下:
#-*- coding:utf-8 -*-
import os
class CodeConvert :
def __init__(self, path, dcode, scode) :
self.file_lists = []
if os.path.exists(path) :
self.parent_path = path
else :
self.parent_path = ""
self.dest_code = dcode
self.src_code = scode
self.exts = [".cpp", ".c", ".h"]
def ListFiles(self) :
for root, dirs, files in os.walk(self.parent_path) :
for name in files :
self.file_lists.append(os.path.join(root, name))
def Convert2DestCode(self) :
for file in self.file_lists:
ext = os.path.splitext(file)[1]
ext = ext.lower()
if ext in self.exts :
self.Convert(file)
def Convert(self, file) :
try :
print file
f = open(file)
buf = f.read()
f.close()
u1 = buf.decode(self.src_code)
utf1 = u1.encode(self.dest_code)
f = open(file, 'w+')
f.write(utf1)
f.close()
except :
print "except" + file
def main() :
convert = CodeConvert('/Users/xxx/Desktop/xxx', 'utf8', 'gbk')
convert.ListFiles()
convert.Convert2DestCode()
python利用py2exe生成的exe窗口一闪而过解决办法
2016-12-1 python
利用py2exe把python脚本生成exe后,运行程序一闪而过
窗口提示找不到模块例如xlsxwriter
原因是xlsxwriter库使在egg里面,因此py2exe生成时没办法导入
解决办法:
把xlsxwriter解压到工程目录文件夹中,再重新生成下就可以了
标签: python py2exe xlsxwriter
python yield 使用说明
2016-10-15 python
python with 使用说明
2016-10-15 python
参考:http://effbot.org/zone/python-with-statement.htm
一旦知道with段用于解决什么问题,那使用with语法将会非常简单:
如下片段:
set things up #打开一个文件或请求外部资源
try :
do something
finally :
tear things down #关闭文件或释放资源
try-finally结构保证"tear things down"一直都会被执行,不管"do something"有没有完成
如果要经常使用这个结构,可以考虑使用如下结构:
def controlled_execution(callback) :
set things up
try :
callback(thing)
finally :
tear things down
def my_fn(thing) :
do something
controlled_execution(my_fn)
但是看起来有点啰嗦, 也可以采用如下方式:
def controlled_execution() :
set things up
try :
yield thing
finally :
tear things down
for thing in controlled_execution() :
do something in thing
但你只要执行一次函数时,也要使用loop就有点奇怪了
python团队使用如下结构来替代:
class controlled_execution :
def __enter__(self) :
set things up
return thing
def __exit__(self, type, value, traceback) :
tear things down
with controlled_execution() as thing :
some code
当with执行时,python执行__enter__方法,返回值给thing,
然后执行some code, 不管some code执行成功与否,执行完调用__exit__方法;
如果要抑制异常抛出,可以使__exit__()返回True,如忽略TypeError:
def __exit__(self, type, value, traceback) :
return isinstance(value, TypeError)
打开一个文件,处理里面内容然后保证它被关闭,可以使用如下代码:
with open("x.txt") as f :
data = f.read()
do something with data
python 解决‘ascii‘ codec can‘t encode characters in position ordinal not in range(128)
2016-10-10 python
使用java 通过cmd调用python脚本时出现unicodeEncodeError
通过搜索说是读取文件默认是ascii而不是utf8导致的
在代码中加上如下几句即可
import sys
reload(sys)
sys.setdefaultencoding(‘utf8’)
标签: python
python dir_util copy_tree errno 9
2016-10-9 python
使用python2.7.10写脚本从一个文件夹复制文件到另一个文件夹时出错
错误原因在读取源文件时找不到文件描述
errno 9
但是通过python idle执行可以,后来把python更新到2.7.12版本就可以了
可能是2.7.10 bug吧
运行环境 windows 2008 64位 英文版
标签: python