Python 使用cURL读取CSV文件以创建Restful API
我正在尝试开发一种服务,有人可以将csv文件发送到我的RESTAPI,我将其转储到数据库中。我的Python 使用cURL读取CSV文件以创建Restful API,python,python-3.x,curl,flask,Python,Python 3.x,Curl,Flask,我正在尝试开发一种服务,有人可以将csv文件发送到我的RESTAPI,我将其转储到数据库中。我的cURL请求正在读取数据,但flask\u restful无法处理它。你能告诉我我做错了什么吗?我怎样才能纠正它 [编辑如下] 我在阅读文档后发现,request.files允许您从表单的POST请求中读取文件。我还找到了一种通过cURL以表单形式发送csv文件的方法 class ForBetaAndUpload(Resource): def post(self, kind='quot
cURL
请求正在读取数据,但flask\u restful
无法处理它。你能告诉我我做错了什么吗?我怎样才能纠正它
[编辑如下]
我在阅读文档后发现,request.files
允许您从表单的POST
请求中读取文件。我还找到了一种通过cURL
以表单形式发送csv文件的方法
class ForBetaAndUpload(Resource):
def post(self, kind='quotes'):
# parser = reqparse.RequestParser()
file = request.files['file']
print(file)
# kind = parser.add_argument('kind').parse_args()['kind']
if kind:
if file and file[-3:]=='csv':
if kind == 'quotes':
try:
df = pd.read_csv(file)
df.to_sql('QUOTES', helper.conx, index=False, if_exists='append')
return jsonify({'message':'%s rows inserted in the databasee table successfully' %(df.shape[0])})
except Exception as e:
return jsonify({'exception': e})
if kind == 'trace':
try:
df = pd.read_csv(todos)
df.iloc[:10].to_sql('TRACE', helper.conx, index=False, if_exists='append')
return jsonify({'message':'%s rows inserted in the databasee table successfully' %(df.shape[0])})
except Exception as e:
return jsonify({'message': e})
else:
return jsonify({'message': 'Please provide a csv file'})
else:
return jsonify({'message':'Please provide the file for to update relevant table in the databse'})
api.add_resource(ForBetaAndUpload, '/upload', endpoint='upload')
if __name__ == "__main__":
app.run(debug=True)
卷曲请求:
curl "https://localhost:5000/upload" -X POST -H 'Content-Type: txt/csv' -d trace.csv --insecure
我收到了以下消息:
curl "https://localhost:5000/upload" -X POST -H 'Content-Type: txt/csv' -d trace.csv --insecure
curl:(35)错误:140770FC:SSL例程:SSL23\u GET\u SERVER\u HELLO:unknown
协议
API错误消息
代码400,消息错误HTTP/0.9请求类型
('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03\x08Ú:ü^Ù~7W\x9fDyy\x16j\x7fõ>½\x82\x90uÎZ\x08êE\x00\x00')
如何将csv文件发送到flask
restful\u api
。我现在做的是对的还是有其他方法 我从Flask
读取csv的解决方案是:
在烧瓶中:
f = request.files['file']
f
将是文件的处理程序,然后您可以使用csv\u阅读器
并发送文件:
curl -s "http://localhost:5000" \
-F file=@./myfile.csv \
-X POST \
-H 'enctype:multipart/form-data ; Content-Type:multipart/form-data'
这应该行得通。让我知道。尝试使用
curl-k
绕过SSLcert@ThanhNguyenVan:我试过curl-K“https://localhost:5000/upload“-X POST-H”内容类型:text/csv;charset=utf-8'-d@trace.csv--不安全
,现在我收到警告:尝试从“”读取配置时出错警告:文件卷曲:未指定URL!curl:try'curl--help'或'curl--manual'了解更多信息-k是小写,为什么使用httpS?对不起,我的错。因此,我将其更改为-k
并使用http
,但现在我发现浏览器(或代理)发送了一个此服务器无法理解的请求。“
在这里找到了解决方案吗?