tags:

views:

41

answers:

1
conn = httplib.HTTPConnection("www.encodable.com/uploaddemo/")
    conn.request("POST",path, chunk,headers)

Above is the site "www.encodable.com/uploaddemo/" where I want to upload an image. Now problem which I am facing is I am tyro in php so I am unbale to understand the meaning of path and headers in above code, chunk is an object that consist of my image file. Following code ofcourse produces error as I was trying to implement without any knowledge of headers and path

import httplib

def upload_image_to_url():

filename = '//home//harshit//Desktop//h1.jpg'
f = open(filename,"rb")
    chunk = f.read()
    f.close()

headers = {”Content−type” : ”application/octet−stream”,”Accept”:”text/plain”}

conn = httplib.HTTPConnection("www.encodable.com/uploaddemo/")
    conn.request("POST","/uploaddemo/files/", chunk)

response = conn.getresponse()
    remote_file = response.read()
    conn.close()
print remote_file

upload_image_to_url()

A: 

Currently, you aren't using the headers you've declared earlier in the code. You should provide them as the fourth argument to conn.request; i.e.,

conn.request("POST","/uploaddemo/files/", chunk, headers)

Also, side note: you can pass open("h1.jpg", "rb") directly into conn.request without reading it fully into chunk first. conn.request accepts file-like objects and it will be more efficient to stream the file a little at a time.

gilesc
That is what I am trying to ask I don't know how to put headers
Harshit Sharma
And I'm telling you that you properly made the `headers` dictionary, but you have to pass it to your `conn.request` function in order for it to do anything.
gilesc