原标题:python上传图片代码
content:
下面是python使用pycurl上传图片的代码
# -*- coding:utf-8 -*-
import pycurl
import urllib
from StringIO import StringIO
#简陋curl
def myCurl(url,COOKIEs='',postdata={},uploadfiles={}):
c = pycurl.Curl()
c.setopt(pycurl.USERAGENT,'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/55.0')
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.FOLLOWLOCATION, 1)
if(COOKIEs):
c.setopt(pycurl.COOKIE,COOKIEs)
c.setopt(pycurl.URL,url)
buffer=StringIO()
c.setopt(c.WRITEDATA, buffer)
if(postdata):
postfields=urllib.urlencode(postdata)
c.setopt(c.POSTFIELDS, postfields)
if(uploadfiles):
c.setopt(c.HTTPPOST, [
(uploadfiles['name'], (
# upload the contents of this file
c.FORM_FILE, uploadfiles['path'],
)),
])
res=c.perform()
data={'c':c,'data':buffer.getvalue()}
#c.close() # getinfo must be called before close.
return data返回搜狐,查看更多
责任编辑: