{"id":155552,"date":"2020-09-02T13:44:26","date_gmt":"2020-09-02T05:44:26","guid":{"rendered":"http:\/\/4563.org\/?p=155552"},"modified":"2020-09-02T13:44:26","modified_gmt":"2020-09-02T05:44:26","slug":"%e5%b0%86%e4%b8%80%e4%b8%aa-flask-api-%e9%87%8c%e6%8e%a5%e6%94%b6%e7%9a%84%e6%96%87%e4%bb%b6%ef%bc%8c%e5%86%8d%e4%bd%bf%e7%94%a8-requests-post-%e5%88%b0%e5%8f%a6%e4%b8%80%e4%b8%aa-flask-api","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=155552","title":{"rendered":"\u5c06\u4e00\u4e2a flask api \u91cc\u63a5\u6536\u7684\u6587\u4ef6\uff0c\u518d\u4f7f\u7528 requests post \u5230\u53e6\u4e00\u4e2a flask api"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5c06\u4e00\u4e2a flask api \u91cc\u63a5\u6536\u7684\u6587\u4ef6\uff0c\u518d\u4f7f\u7528 requests post \u5230\u53e6\u4e00\u4e2a flask api               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : j0shfan <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u5982\u9898\uff0c\u7b80\u800c\u8a00\u4e4b\uff0c\u5c31\u662f\u901a\u8fc7 flask.request \u63a5\u6536\u6587\u4ef6\uff0c\u5728\u7528 requests post \u5230\u53e6\u5916\u4e00\u4e2a flask api \u7684 url \u4e0a<\/p>\n<p>\u6211\u7684 requests post \u4ee3\u7801\u5982\u4e0b<br \/>responses_of_my_request = requests.post(url=another_flask_url, data=other_data, files={&#8216;file&#8217;:(&#8216;file.jpg&#8217;,file_data,&#8217;image\/jpeg&#8217;)})<\/p>\n<p>\u5176\u4e2d\u7684 file_data \u5728\u6587\u6863\u4e2d\u662f\u4f7f\u7528 open(&#8216;file_name.XXX&#8217;, &#8216;rb&#8217;)\u6765\u6253\u5f00\u7684\uff0c\u4f46\u5728\u6211\u7684\u5e94\u7528\u4e2d\u6211\u9700\u8981\u5c06\u4e00\u4e2a\u4ece flask api \u4e0a\u4f20\u4e0a\u6765\u7684\u6587\u4ef6\u6570\u636e\u653e\u5728\u8fd9\u91cc<br \/>\u8fd9\u4e2a file_data \u6211\u5c1d\u8bd5\u901a\u8fc7 flask.request.files[&#8216;my_file&#8217;]\u53d6\u5f97\uff0c\u4e5f\u5c1d\u8bd5\u901a\u8fc7 flask.request.files[&#8216;my_file&#8217;].stream.write()\u53d6\u5f97<br \/>\u8c03\u8bd5\u4e2d\uff0c\u53d1\u73b0\u6700\u540e\u63a5\u6536\u7aef flask api \u53ea\u80fd\u6536\u5230 data=other_data \u8fd9\u90e8\u5206\uff0c\u800c\u6536\u4e0d\u5230\u6587\u4ef6<br \/>\u68c0\u67e5\uff1aflask.request.files[file_field]\u4e0b\u6587\u4ef6\u540d&#8217;file.jpg&#8217;\u548c content_type &#8216;image\/jpeg&#8217;\u90fd\u53ef\u4ee5\u63a5\u6536\u5230\uff0c\u5c31\u662f content_length \u4e3a 0 \uff08\u4f46\u662f\uff0c\u6211\u68c0\u67e5\u4e86\u4e0b\u5176\u4ed6\u80fd\u6210\u529f\u4e0a\u4f20\u6587\u4ef6\u7684 api\uff0c\u53d1\u73b0\u4e5f\u4e3a 0\uff0c\u6240\u4ee5\u95ee\u9898\u4e0d\u4e00\u5b9a\u51fa\u5728\u8fd9\u91cc\uff09<\/p>\n<p>\u8bf7\u6559\u5404\u4f4d\u5927\u4f6c\uff01      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3295523\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             files = {&#8220;file&#8221;: (&#8220;file.name&#8221;, open(&#8220;file&#8221;, &#8220;rb&#8221;), &#8220;image\/jpeg&#8221;)}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3295524\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : est <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u662f\u540c\u4e00\u4e2a flask \u9879\u76ee\u91cc\u7684\uff0c\u4e3a\u5565\u4e0d\u628a\u8fd9\u6bb5\u4ee3\u7801\u63d0\u53d6\u51fa\u6765\u5171\u7528\u5462?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3295525\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : j0shfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @est \u90a3\u6bb5 api \u4ee3\u7801\u592a\u957f\u4e86\uff0c\u4ee5\u540e\u6709\u4fee\u6539\u7684\u8bdd\uff0c\u6539\u597d\u51e0\u4e2a\u5730\u65b9\u6015\u5fd8\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3295526\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : j0shfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @18870715400 \u53ef\u662f\u6211\u5e76\u6ca1\u6709\u628a\u7b2c\u4e00\u4e2a api \u63a5\u6536\u5230\u7684\u6587\u4ef6\u5b58\u5728\u6587\u4ef6\u7cfb\u7edf\u91cc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3295527\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : learningman <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3a\u5565\u4e0d\u62bd\u8c61\u6210\u51fd\u6570\uff0c\u975e\u8981\u8fd9\u6837\u5360\u7528\u81ea\u5df1\u7684\u56de\u73af IO                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3295528\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : j0shfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @learningman flask \u4ed6\u6bcf\u4e2a api \u786e\u5b9e\u662f\u4e2a\u51fd\u6570\uff0c\u4f46\u8981\u4e92\u76f8\u8c03\u7528\u7684\u8bdd\u8c8c\u4f3c\u53ea\u770b\u5230\u6709\u8fd9\u79cd\u5bf9\u53e6\u4e00\u4e2a api \u53d1\u8bf7\u6c42\u7684\u4f8b\u5b50                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3295529\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : frostming <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @j0shfan <\/p>\n<p>flask \u4ed6\u6bcf\u4e2a api \u786e\u5b9e\u662f\u4e2a\u51fd\u6570\uff0c\u4f46\u8981\u4e92\u76f8\u8c03\u7528\u7684\u8bdd\u8c8c\u4f3c\u53ea\u770b\u5230\u6709\u8fd9\u79cd\u5bf9\u53e6\u4e00\u4e2a api \u53d1\u8bf7\u6c42\u7684\u4f8b\u5b50<\/p>\n<p>\u90a3\u4f60\u4e3a\u4ec0\u4e48\u4e0d\u628a\u5904\u7406\u6570\u636e\u7684\u90e8\u5206\u62bd\u51fa\u6765\u51fd\u6570\uff0c\u8131\u79bb api \u800c\u5b58\u5728\uff0capi \u51fd\u6570\u5c31\u76f4\u63a5\u8c03\u5c31\u597d\u4e86                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u4e00\u4e2a flask api \u91cc\u63a5\u6536&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155552"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155552"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155552\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155552"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}