{"id":1858,"date":"2011-08-12T16:13:50","date_gmt":"2011-08-12T16:13:50","guid":{"rendered":"https:\/\/supportex.net\/?p=1858"},"modified":"2020-04-15T13:18:33","modified_gmt":"2020-04-15T11:18:33","slug":"python-uploading-file-http","status":"publish","type":"post","link":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/","title":{"rendered":"Python: uploading file via HTTP with pyCurl and Requests"},"content":{"rendered":"<p>Here is a simple way to upload file in Python. You should have PyCurl installed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import pycurl\r\nfrom cStringIO import StringIO\r\nfilename='test_file'\r\nc = pycurl.Curl()\r\nc.setopt(c.POST, 1)\r\nc.setopt(c.HTTPPOST, [('title', 'test'), (('file', (c.FORM_FILE, filename)))])\r\nc.setopt(c.VERBOSE, 1)\r\nbodyOutput = StringIO()\r\nheadersOutput = StringIO()\r\nc.setopt(c.WRITEFUNCTION, bodyOutput.write)\r\nc.setopt(c.URL, \"http:\/\/upload.example.com\" )\r\nc.setopt(c.HEADERFUNCTION, headersOutput.write)\r\nc.perform()\r\nprint bodyOutput.getvalue()\r\n<\/pre>\n<p><strong>Upd.<br \/>\n<\/strong>File upload with <a href=\"http:\/\/docs.python-requests.org\/en\/latest\/\">Requests<\/a> by Kenneth Reitz:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import requests\r\nfilename='test_file'\r\nf = open (filename)\r\nr =  requests.post(url='http:\/\/upload.example.com', data =  {'title':'test_file'},  files =  {'file':f})\r\nprint r.status_code\r\nprint r.headers\r\n<\/pre>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here is a simple way to upload file in Python. You should have PyCurl installed. import pycurl from cStringIO import StringIO filename=&#8217;test_file&#8217; c = pycurl.Curl() c.setopt(c.POST, 1) c.setopt(c.HTTPPOST, [(&#8216;title&#8217;, &#8216;test&#8217;), ((&#8216;file&#8217;, (c.FORM_FILE, filename)))]) c.setopt(c.VERBOSE, 1) bodyOutput = StringIO() headersOutput = StringIO() c.setopt(c.WRITEFUNCTION, bodyOutput.write) c.setopt(c.URL, &#8220;http:\/\/upload.example.com&#8221; ) c.setopt(c.HEADERFUNCTION, headersOutput.write) c.perform() print bodyOutput.getvalue() Upd. File upload with&hellip; <\/p>\n<div class=\"readmore-wrapper\"><a href=\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\" class=\"more-link\">Read <\/a><\/div>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[275,5],"class_list":["post-1858","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python: uploading file via HTTP with pyCurl and Requests &#8211; Supportex.NET blog<\/title>\n<meta name=\"description\" content=\"How to upload file via HTTP with pyCurl and Requests\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: uploading file via HTTP with pyCurl and Requests &#8211; Supportex.NET blog\" \/>\n<meta property=\"og:description\" content=\"How to upload file via HTTP with pyCurl and Requests\" \/>\n<meta property=\"og:url\" content=\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\" \/>\n<meta property=\"og:site_name\" content=\"Supportex.NET blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-08-12T16:13:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-15T11:18:33+00:00\" \/>\n<meta name=\"author\" content=\"Oleksii Tykhonov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleksii Tykhonov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\"},\"author\":{\"name\":\"Oleksii Tykhonov\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"headline\":\"Python: uploading file via HTTP with pyCurl and Requests\",\"datePublished\":\"2011-08-12T16:13:50+00:00\",\"dateModified\":\"2020-04-15T11:18:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\"},\"wordCount\":32,\"commentCount\":2,\"keywords\":[\"linux\",\"python\"],\"articleSection\":[\"linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\",\"url\":\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\",\"name\":\"Python: uploading file via HTTP with pyCurl and Requests &#8211; Supportex.NET blog\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\"},\"datePublished\":\"2011-08-12T16:13:50+00:00\",\"dateModified\":\"2020-04-15T11:18:33+00:00\",\"author\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"description\":\"How to upload file via HTTP with pyCurl and Requests\",\"breadcrumb\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/supportex.net\/blog\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: uploading file via HTTP with pyCurl and Requests\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\",\"url\":\"https:\/\/supportex.net\/blog\/en\/\",\"name\":\"Supportex.NET blog\",\"description\":\"Server and network management company\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\",\"name\":\"Oleksii Tykhonov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"caption\":\"Oleksii Tykhonov\"},\"url\":\"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: uploading file via HTTP with pyCurl and Requests &#8211; Supportex.NET blog","description":"How to upload file via HTTP with pyCurl and Requests","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/","og_locale":"en_US","og_type":"article","og_title":"Python: uploading file via HTTP with pyCurl and Requests &#8211; Supportex.NET blog","og_description":"How to upload file via HTTP with pyCurl and Requests","og_url":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/","og_site_name":"Supportex.NET blog","article_published_time":"2011-08-12T16:13:50+00:00","article_modified_time":"2020-04-15T11:18:33+00:00","author":"Oleksii Tykhonov","twitter_misc":{"Written by":"Oleksii Tykhonov","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#article","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/"},"author":{"name":"Oleksii Tykhonov","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"headline":"Python: uploading file via HTTP with pyCurl and Requests","datePublished":"2011-08-12T16:13:50+00:00","dateModified":"2020-04-15T11:18:33+00:00","mainEntityOfPage":{"@id":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/"},"wordCount":32,"commentCount":2,"keywords":["linux","python"],"articleSection":["linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/","url":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/","name":"Python: uploading file via HTTP with pyCurl and Requests &#8211; Supportex.NET blog","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/en\/#website"},"datePublished":"2011-08-12T16:13:50+00:00","dateModified":"2020-04-15T11:18:33+00:00","author":{"@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"description":"How to upload file via HTTP with pyCurl and Requests","breadcrumb":{"@id":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/supportex.net\/blog\/2011\/08\/python-uploading-file-http\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/supportex.net\/blog\/en\/"},{"@type":"ListItem","position":2,"name":"Python: uploading file via HTTP with pyCurl and Requests"}]},{"@type":"WebSite","@id":"https:\/\/supportex.net\/blog\/en\/#website","url":"https:\/\/supportex.net\/blog\/en\/","name":"Supportex.NET blog","description":"Server and network management company","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251","name":"Oleksii Tykhonov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","caption":"Oleksii Tykhonov"},"url":"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/"}]}},"_links":{"self":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/1858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/comments?post=1858"}],"version-history":[{"count":3,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/1858\/revisions"}],"predecessor-version":[{"id":3328,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/1858\/revisions\/3328"}],"wp:attachment":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/media?parent=1858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/categories?post=1858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/tags?post=1858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}