python pycurl模块发送post请求与上传文件

发布时间:2020-02-01编辑:脚本学堂
python pycurl模块发送post请求,python pycurl模块上传文件的例子。

python pycurl模块上传图片的函数
 

复制代码 代码示例:
def upload_img(self, mypic):
        '''
        upload the image to the remote server.
        '''
 
        storage = StringIO()
        c = pycurl.Curl()
        values = [
                  ("upload_file[]", (pycurl.FORM_FILE, str(mypic)))
                  ]
        c.setopt(c.URL, self.upload_url)
        c.setopt(c.WRITEFUNCTION, storage.write)
        c.setopt(c.HTTPPOST, values)
        c.perform()
        c.close()
        content = storage.getvalue()
        print content
 

以上用到了python pycurl模块,代码中mypic就是一个本地图片的路径,storage变量用于存储post后返回的相应信息。

完整代码:https://github.com/smilejay/python/blob/master/py2014/app_poi.py

参考资料:
http://pycurl.cvs.sourceforge.net/pycurl/pycurl/tests/test_post2.py?view=markup