def __init__(self, host, port, enable_ssl): """ @param host: The host to connect to @type conn: str @param port: The port on the host to connect to @type port: int @param enable_ssl: True if we should use HTTPS, otherwise False @type enable_ssl: bool """ if enable_ssl: from httplib import HTTPSConnection as Connection else: from httplib import HTTPConnection as Connection self.conn = Connection(host, port) self.host = host self.port = port self.enable_ssl = enable_ssl self.auth_header = None # Try to fetch the pre-set username/password for this server import commands import re output = commands.getstatusoutput( "security find-internet-password -gs %s" % self.host) if output[0] == 0: for l in output[1].split("\n"): matches = re.match("password: \"(.+?)\"", str(l)) if matches: password = matches.group(1) matches = re.match("\s+?\"acct\"<blob>=\"(.+?)\"", str(l)) if matches: username = matches.group(1) self.set_basic_auth(username, password)
def _connect(self): if self.db_host: if self.enable_ssl: from httplib import HTTPSConnection as Connection else: from httplib import HTTPConnection as Connection self.connection = Connection(self.db_host, self.db_port)
#!/usr/bin/env python from httplib import HTTPSConnection as Connection import time import sys # send data conn = Connection(sys.argv[1]) # example.com:8090 conn.request('GET', '/', headers={}) resp = conn.getresponse() total_len = int(resp.getheader('Content-Length')) out = [] for i in range(total_len): out.append(resp.read(1)) sys.stdout.write('\r%d' % i) sys.stdout.flush() time.sleep(2) conn.close() print ''.join(out)