Make an http POST request to upload a file using Python urllib/urllib2

前端 未结 3 1872
一整个雨季
一整个雨季 2020-12-13 20:39

I would like to make a POST request to upload a file to a web service (and get response) using Python. For example, I can do the following POST request with curl

3条回答
  •  难免孤独
    2020-12-13 21:35

    Personally I think you should consider the requests library to post files.

    url = 'http://jigsaw.w3.org/css-validator/validator'
    files = {'file': open('style.css')}
    response = requests.post(url, files=files)
    

    Uploading files using urllib2 is not impossible but quite a complicated task: http://pymotw.com/2/urllib2/#uploading-files

提交回复
热议问题