-
-
Save alecordev/baf60c3a1f4bbce3a2a902600af169bf to your computer and use it in GitHub Desktop.
pycurl examples with Kerberos/NTLM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import json | |
from pprint import pprint | |
import pycurl | |
def post(url, data): | |
buffer = io.BytesIO() | |
c = pycurl.Curl() | |
c.setopt(c.URL, url) | |
c.setopt(c.HTTPAUTH, c.HTTPAUTH_GSSNEGOTIATE) | |
c.setopt(c.USERPWD, ':') | |
c.setopt(c.HTTPHEADER, ['Content-Type: application/json']) | |
c.setopt(c.USERAGENT, 'Python Client') | |
c.setopt(c.POSTFIELDS, json.dumps(data).encode()) | |
c.setopt(c.POST, 1) | |
# c.setopt(c.HTTPPOST, data) | |
# c.setopt(c.READFUNCTION, f) | |
# c.setopt(c.READDATA, data) | |
# c.setopt(c.VERBOSE, True) | |
c.setopt(c.WRITEDATA, buffer) | |
c.perform() | |
print(c.getinfo(c.RESPONSE_CODE)) | |
print('Elapsed time: {}'.format(c.getinfo(c.TOTAL_TIME))) | |
c.close() | |
pprint(json.loads(buffer.getvalue().decode())) | |
if __name__ == '__main__': | |
post() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pycurl | |
try: | |
from io import BytesIO | |
except ImportError: | |
from StringIO import StringIO as BytesIO | |
_proxy_url = 'proxy.com:80' | |
_proxy_port = 80 | |
def fetch_url(url): | |
buf = BytesIO() | |
c = pycurl.Curl() | |
c.setopt(c.URL, url) | |
c.setopt(pycurl.SSL_VERIFYPEER, 0) | |
c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH) | |
c.setopt(pycurl.PROXY, _proxy_url) | |
c.setopt(pycurl.PROXYPORT, _proxy_port) | |
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP) | |
c.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_NTLM) | |
c.setopt(pycurl.PROXYUSERNAME, '') | |
c.setopt(pycurl.PROXYPASSWORD, '') | |
c.setopt(c.WRITEDATA, buf) | |
c.perform() | |
c.close() | |
body = buf.getvalue() | |
return body | |
def fetch_file(url, save_path): | |
with open(save_path, 'wb') as f: | |
c = pycurl.Curl() | |
c.setopt(c.URL, url) | |
c.setopt(pycurl.SSL_VERIFYPEER, 0) | |
c.setopt(pycurl.SSL_VERIFYPEER, 0) | |
c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH) | |
c.setopt(pycurl.PROXY, _proxy_url) | |
c.setopt(pycurl.PROXYPORT, _proxy_port) | |
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP) | |
c.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_NTLM) | |
c.setopt(pycurl.PROXYUSERNAME, '') | |
c.setopt(pycurl.PROXYPASSWORD, '') | |
c.setopt(c.WRITEDATA, f) | |
c.perform() | |
c.close() | |
def fetch_url_ntlm(url): | |
buf = BytesIO() | |
c = pycurl.Curl() | |
c.setopt(c.URL, url) | |
c.setopt(pycurl.SSL_VERIFYPEER, 0) | |
user = '' | |
password = '' | |
c.setopt(pycurl.PROXY, _proxy_url) | |
c.setopt(pycurl.PROXYPORT, _proxy_port) | |
c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_NEGOTIATE) | |
c.setopt(pycurl.USERPWD, '{}:{}'.format(user, password)) | |
c.setopt(c.WRITEDATA, buf) | |
c.perform() | |
c.close() | |
return buf.getvalue().decode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment