Curl POST request using pycurl

702 views Asked by At

someone can me explain how can i use curl request below in pycurl library:

curl -X POST --user 'username:password' url

Thanks.

1

There are 1 answers

5
Chris Larson On BEST ANSWER

This may be answering your question with a suggestion, but without knowing your reasons for using PyCurl, I'm going to suggest you use the 'requests' module.

PyCurl is useful for very high network-intensive tasks, but otherwise, the 'requests' module is vastly easier and less painful to use.

To do as you ask with requests, the following works:

import requests

url = 'http://www.yoururl.here'

requests.post('url', auth=('username', 'password'))

Or, to be a little safer and better informed, use:

import requests

try:
    response = requests.post('url', auth=('username', 'password'))
    print('Response HTTP Status Code: {status_code}'.format(
        status_code=response.status_code
        )
    )
    print('Response HTTP Response Body: {content}'.format(
        content=response.content
        )
    )
except requests.exceptions.RequestException:
    print( 'HTTP Request failed', response.content )

Assuming your POST works, the above isn't strictly necessary, nor is it particular to using 'requests', but it will let you know how things went with your request went. It's nothing but a try: except: approach to the same thing, with some print() statements to report back how everything worked out. :)

And assuming you have data to post, 'requests' makes that simple as well. See the following for more comprehensive information:

http://docs.python-requests.org/en/master/user/quickstart/

If you are constrained to using PyCurl, though, have you looked at their Quickstart page? The last couple or three sections, in particular, may help:

http://pycurl.io/docs/latest/quickstart.html