Trying to post multipart form data in python, won't post

前端 未结 2 2018
刺人心
刺人心 2021-01-06 08:22

I\'m fairly new to python, so I apologize in advance if this is something simple I\'m missing. I\'m trying to post data to a multipart form in python. The script runs, but i

相关标签:
2条回答
  • 2021-01-06 08:40
    import urllib, urllib2
    from poster.encode import multipart_encode
    from poster.streaminghttp import register_openers
    
    def queXF():
        register_openers()
        url = "http://lilix2/trunk/admin/new.php"
        values = {'form':open('test.pdf'),
              'bandingxml':open('banding.xml'),
              'desc':'description'}
        data, headers = multipart_encode(values)
        headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        request = urllib2.Request(url, data, headers)
        request.unverifiable = True
        response = urllib2.urlopen(request)
        the_page = response.read()
    

    Adding headers['User-Agent'] and request.unverifiable = True seems to have fixed it.

    0 讨论(0)
  • 2021-01-06 08:42

    Try using the requests library. Docs on posting multipart file are here: http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

    0 讨论(0)
提交回复
热议问题