Python socks.HTTP Examples

The following are 30 code examples of socks.HTTP(). 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 socks , or try the search function .
Example #1
Source File: connect_creator.py    From XX-Net-mini with GNU General Public License v3.0 8 votes vote down vote up
def update_config(self):
        if int(self.config.PROXY_ENABLE):

            if self.config.PROXY_TYPE == "HTTP":
                proxy_type = socks.HTTP
            elif self.config.PROXY_TYPE == "SOCKS4":
                proxy_type = socks.SOCKS4
            elif self.config.PROXY_TYPE == "SOCKS5":
                proxy_type = socks.SOCKS5
            else:
                self.logger.error("proxy type %s unknown, disable proxy", self.config.PROXY_TYPE)
                raise Exception()

            socks.set_default_proxy(proxy_type, self.config.PROXY_HOST, self.config.PROXY_PORT,
                                    self.config.PROXY_USER,
                                    self.config.PROXY_PASSWD) 
Example #2
Source File: connect_manager.py    From XX-Net-mini with GNU General Public License v3.0 7 votes vote down vote up
def load_proxy_config():
    global default_socket
    if g.config.PROXY_ENABLE:

        if g.config.PROXY_TYPE == "HTTP":
            proxy_type = socks.HTTP
        elif g.config.PROXY_TYPE == "SOCKS4":
            proxy_type = socks.SOCKS4
        elif g.config.PROXY_TYPE == "SOCKS5":
            proxy_type = socks.SOCKS5
        else:
            xlog.error("proxy type %s unknown, disable proxy", g.config.PROXY_TYPE)
            raise Exception()

        socks.set_default_proxy(proxy_type, g.config.PROXY_HOST, g.config.PROXY_PORT,
                                g.config.PROXY_USER, g.config.PROXY_PASSWD) 
Example #3
Source File: _http.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
def _tunnel(sock, host, port, auth):
    debug("Connecting proxy...")
    connect_header = "CONNECT %s:%d HTTP/1.0\r\n" % (host, port)
    # TODO: support digest auth.
    if auth and auth[0]:
        auth_str = auth[0]
        if auth[1]:
            auth_str += ":" + auth[1]
        encoded_str = base64encode(auth_str.encode()).strip().decode()
        connect_header += "Proxy-Authorization: Basic %s\r\n" % encoded_str
    connect_header += "\r\n"
    dump("request header", connect_header)

    send(sock, connect_header)

    try:
        status, resp_headers, status_message = read_headers(sock)
    except Exception as e:
        raise WebSocketProxyException(str(e))

    if status != 200:
        raise WebSocketProxyException(
            "failed CONNECT via proxy status: %r" % status)

    return sock 
Example #4
Source File: _http.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def _tunnel(sock, host, port, auth):
    debug("Connecting proxy...")
    connect_header = "CONNECT %s:%d HTTP/1.0\r\n" % (host, port)
    # TODO: support digest auth.
    if auth and auth[0]:
        auth_str = auth[0]
        if auth[1]:
            auth_str += ":" + auth[1]
        encoded_str = base64encode(auth_str.encode()).strip().decode().replace('\n', '')
        connect_header += "Proxy-Authorization: Basic %s\r\n" % encoded_str
    connect_header += "\r\n"
    dump("request header", connect_header)

    send(sock, connect_header)

    try:
        status, resp_headers, status_message = read_headers(sock)
    except Exception as e:
        raise WebSocketProxyException(str(e))

    if status != 200:
        raise WebSocketProxyException(
            "failed CONNECT via proxy status: %r" % status)

    return sock 
Example #5
Source File: rainbow.py    From rainbowstream with MIT License 6 votes vote down vote up
def proxy_connect(args):
    """
    Connect to specified proxy
    """
    if args.proxy_host:
        # Setup proxy by monkeypatching the standard lib
        if args.proxy_type.lower() == "socks5" or not args.proxy_type:
            socks.set_default_proxy(
                socks.SOCKS5, args.proxy_host,
                int(args.proxy_port))
        elif args.proxy_type.lower() == "http":
            socks.set_default_proxy(
                socks.HTTP, args.proxy_host,
                int(args.proxy_port))
        elif args.proxy_type.lower() == "socks4":
            socks.set_default_proxy(
                socks.SOCKS4, args.proxy_host,
                int(args.proxy_port))
        else:
            printNicely(
                magenta('Sorry, wrong proxy type specified! Aborting...'))
            sys.exit()
        socket.socket = socks.socksocket 
Example #6
Source File: _http.py    From launcher with GNU General Public License v3.0 6 votes vote down vote up
def _tunnel(sock, host, port, auth):
    debug("Connecting proxy...")
    connect_header = "CONNECT %s:%d HTTP/1.0\r\n" % (host, port)
    # TODO: support digest auth.
    if auth and auth[0]:
        auth_str = auth[0]
        if auth[1]:
            auth_str += ":" + auth[1]
        encoded_str = base64encode(auth_str.encode()).strip().decode()
        connect_header += "Proxy-Authorization: Basic %s\r\n" % encoded_str
    connect_header += "\r\n"
    dump("request header", connect_header)

    send(sock, connect_header)

    try:
        status, resp_headers, status_message = read_headers(sock)
    except Exception as e:
        raise WebSocketProxyException(str(e))

    if status != 200:
        raise WebSocketProxyException(
            "failed CONNECT via proxy status: %r" % status)

    return sock 
Example #7
Source File: shared_client_tests.py    From azure-iot-sdk-python with MIT License 6 votes vote down vote up
def test_proxy_options(
        self,
        option_test_required_patching,
        client_create_method,
        create_method_args,
        mock_mqtt_pipeline_init,
        mock_http_pipeline_init,
    ):
        proxy_options = ProxyOptions(proxy_type=socks.HTTP, proxy_addr="127.0.0.1", proxy_port=8888)
        client_create_method(*create_method_args, proxy_options=proxy_options)

        # Get configuration object, and ensure it was used for both protocol pipelines
        assert mock_mqtt_pipeline_init.call_count == 1
        config = mock_mqtt_pipeline_init.call_args[0][0]
        assert isinstance(config, IoTHubPipelineConfig)
        assert config == mock_http_pipeline_init.call_args[0][0]

        assert config.proxy_options is proxy_options 
Example #8
Source File: _http.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _tunnel(sock, host, port, auth):
    debug("Connecting proxy...")
    connect_header = "CONNECT %s:%d HTTP/1.0\r\n" % (host, port)
    # TODO: support digest auth.
    if auth and auth[0]:
        auth_str = auth[0]
        if auth[1]:
            auth_str += ":" + auth[1]
        encoded_str = base64encode(auth_str.encode()).strip().decode()
        connect_header += "Proxy-Authorization: Basic %s\r\n" % encoded_str
    connect_header += "\r\n"
    dump("request header", connect_header)

    send(sock, connect_header)

    try:
        status, resp_headers, status_message = read_headers(sock)
    except Exception as e:
        raise WebSocketProxyException(str(e))

    if status != 200:
        raise WebSocketProxyException(
            "failed CONNECT via proxy status: %r" % status)

    return sock 
Example #9
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def global_override_HTTP_test():
    socks.set_default_proxy(socks.HTTP, "127.0.0.1", 8081)
    good = socket.socket
    socket.socket = socks.socksocket
    status = urllib2.urlopen("http://ifconfig.me/ip").getcode()
    socket.socket = good
    assert status == 200 
Example #10
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def socket_HTTP_test():
    s = socks.socksocket()
    s.set_proxy(socks.HTTP, "127.0.0.1", 8081)
    s.connect(("ifconfig.me", 80))
    s.sendall(raw_HTTP_request())
    status = s.recv(2048).splitlines()[0]
    assert status.startswith(b"HTTP/1.1 200") 
Example #11
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def socket_SOCKS4_test():
    s = socks.socksocket()
    s.set_proxy(socks.SOCKS4, "127.0.0.1", 1080)
    s.connect(("ifconfig.me", 80))
    s.sendall(raw_HTTP_request())
    status = s.recv(2048).splitlines()[0]
    assert status.startswith(b"HTTP/1.1 200") 
Example #12
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def socket_SOCKS5_test():
    s = socks.socksocket()
    s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
    s.connect(("ifconfig.me", 80))
    s.sendall(raw_HTTP_request())
    status = s.recv(2048).splitlines()[0]
    assert status.startswith(b"HTTP/1.1 200") 
Example #13
Source File: _http.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def _open_proxied_socket(url, options, proxy):
    hostname, port, resource, is_secure = parse_url(url)

    if not HAS_PYSOCKS:
        raise WebSocketException("PySocks module not found.")

    ptype = socks.SOCKS5
    rdns = False
    if proxy.type == "socks4":
        ptype = socks.SOCKS4
    if proxy.type == "http":
        ptype = socks.HTTP
    if proxy.type[-1] == "h":
        rdns = True

    sock = socks.create_connection(
            (hostname, port),
            proxy_type = ptype,
            proxy_addr = proxy.host,
            proxy_port = proxy.port,
            proxy_rdns = rdns,
            proxy_username = proxy.auth[0] if proxy.auth else None,
            proxy_password = proxy.auth[1] if proxy.auth else None,
            timeout = options.timeout,
            socket_options = DEFAULT_SOCKET_OPTION + options.sockopt
    )

    if is_secure:
        if HAVE_SSL:
            sock = _ssl_socket(sock, options.sslopt, hostname)
        else:
            raise WebSocketException("SSL not available.")

    return sock, (hostname, port, resource) 
Example #14
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def socket_SOCKS5_auth_test():
    # TODO: add support for this test. Will need a better SOCKS5 server.
    s = socks.socksocket()
    s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081, username="a", password="b")
    s.connect(("ifconfig.me", 80))
    s.sendall(raw_HTTP_request())
    status = s.recv(2048).splitlines()[0]
    assert status.startswith(b"HTTP/1.1 200") 
Example #15
Source File: rainbow.py    From rainbowstream with MIT License 5 votes vote down vote up
def parse_arguments():
    """
    Parse the arguments
    """
    parser = argparse.ArgumentParser(description=__doc__ or "")
    parser.add_argument(
        '-s',
        '--stream',
        default="mine",
        help='Default stream after program start. (Default: mine)')
    parser.add_argument(
        '-to',
        '--timeout',
        help='Timeout for the stream (seconds).')
    parser.add_argument(
        '-tt',
        '--track-keywords',
        help='Search the stream for specific text.')
    parser.add_argument(
        '-fil',
        '--filter',
        help='Filter specific screen_name.')
    parser.add_argument(
        '-ig',
        '--ignore',
        help='Ignore specific screen_name.')
    parser.add_argument(
        '-iot',
        '--image-on-term',
        action='store_true',
        help='Display all image on terminal.')
    parser.add_argument(
        '-24',
        '--color-24bit',
        action='store_true',
        help='Display images using 24bit color codes.')
    parser.add_argument(
        '-ph',
        '--proxy-host',
        help='Use HTTP/SOCKS proxy for network connections.')
    parser.add_argument(
        '-pp',
        '--proxy-port',
        default=8080,
        help='HTTP/SOCKS proxy port (Default: 8080).')
    parser.add_argument(
        '-pt',
        '--proxy-type',
        default='SOCKS5',
        help='Proxy type (HTTP, SOCKS4, SOCKS5; Default: SOCKS5).')
    parser.add_argument(
        '-ta',
        '--twitter-auth',
        default=os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.rainbow_oauth',
        help='Specify which OAuth profile to use for twitter. Default: ~/.rainbow_oauth.')
    parser.add_argument(
        '-pa',
        '--pocket-auth',
        default=os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.rainbow_pckt_oauth',
        help='Specify which OAuth profile to use for pocket. Default: ~/.rainbow_pckt_oauth.')
    return parser.parse_args() 
Example #16
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def socket_SOCKS4_IP_test():
    s = socks.socksocket()
    s.set_proxy(socks.SOCKS4, "127.0.0.1", 1080)
    s.connect(("133.242.129.236", 80))
    s.sendall(raw_HTTP_request())
    status = s.recv(2048).splitlines()[0]
    assert status.startswith(b"HTTP/1.1 200") 
Example #17
Source File: _http.py    From launcher with GNU General Public License v3.0 5 votes vote down vote up
def _open_proxied_socket(url, options, proxy):
    hostname, port, resource, is_secure = parse_url(url)

    if not HAS_PYSOCKS:
        raise WebSocketException("PySocks module not found.")

    ptype = socks.SOCKS5
    rdns = False
    if proxy.type == "socks4":
        ptype = socks.SOCKS4
    if proxy.type == "http":
        ptype = socks.HTTP
    if proxy.type[-1] == "h":
        rdns = True

    sock = socks.create_connection(
            (hostname, port),
            proxy_type = ptype,
            proxy_addr = proxy.host,
            proxy_port = proxy.port,
            proxy_rdns = rdns,
            proxy_username = proxy.auth[0] if proxy.auth else None,
            proxy_password = proxy.auth[1] if proxy.auth else None,
            timeout = options.timeout,
            socket_options = DEFAULT_SOCKET_OPTION + options.sockopt
    )

    if is_secure:
        if HAVE_SSL:
            sock = _ssl_socket(sock, options.sslopt, hostname)
        else:
            raise WebSocketException("SSL not available.")

    return sock, (hostname, port, resource) 
Example #18
Source File: verifier.py    From email-verifier with MIT License 5 votes vote down vote up
def __init__(self,
                 source_addr,
                 proxy_type = None,
                 proxy_addr = None,
                 proxy_port = None,
                 proxy_username = None,
                 proxy_password = None):
        """
        Initializes the Verifier object with proxy settings.
        :param proxy_type: One of `SOCKS4`, `SOCKS5` or `HTTP`.
        :param proxy_addr: Address of the proxy.
        :param proxy_port: Port of the proxy.
        :param proxy_username: Username to authenticate with.
        :param proxy_password: Password for the user. (Only when username is provided)
        """
        if proxy_type:
            try:
                self.proxy_type = proxy[proxy_type.lower()]
            except KeyError as e:
                raise UnknownProxyError(proxy_type)
        else:
            self.proxy_type = None
        self.source_addr = source_addr
        self.proxy_addr = proxy_addr
        self.proxy_port = proxy_port
        self.proxy_username = proxy_username
        self.proxy_password = proxy_password 
Example #19
Source File: gsocket.py    From Galileo with GNU General Public License v3.0 5 votes vote down vote up
def Send(self,target,data=None,port=80,path=''):
		# set socks
		gsock = socks.socksocket()
		# method 
		if self.method != '':method = self.method.upper()
		else: method = 'GET'
		# timeout
		if self.timeout != None:
			print(self.timeout)
			gsock.settimeout(self.timeout)
		# set proxy
		if self.proxy != ('' or None or ""):
			proto,host,port = RProxy(self.proxy)
			if proto == 3:gsock.set_proxy(socks.HTTP,host,port)
			elif proto == 2:gsock.set_proxy(socks.SOCKS5,host,port)
			else:gsock.set_proxy(socks.SOCKS4,host,port)
		# connect
		gsock.connect((target,port))
		# get
		if method == 'GET':
			req  = '\r%s /%s %s/%s\r\n'%(method,data if data != None else '',
				self.protocol.upper(),self.http_version)
			if self.headers != ('' or None):req += '%s'%(RHeader(self.headers))
		# post
		elif method == 'POST':
			req = '\r%s /%s %s/%s\r\n'%(method,data if data != None else '',
				self.protocol.upper(),self.http_version)
			if self.headers != ('' or None):req += '%s'%(RHeader(self.headers))
			if data != ('' or None):req += '\r\n%s\r\n'%(data)
		# other methods
		else:
			req = '\r%s /%s %s/%s\r\n'%(method,data if data != None else '',
				self.protocol.upper(),self.http_version)
			if self.headers != ('' or None):req += '%s'%(RHeader(self.headers))
		# send data
		gsock.sendall(req)
		# return resp
		resp = gsock.recv(4096)
		return resp 
Example #20
Source File: send_message_via_proxy.py    From azure-iot-sdk-python with MIT License 5 votes vote down vote up
def main():
    # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
    conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")

    proxy_opts = ProxyOptions(
        proxy_type=socks.HTTP, proxy_addr="127.0.0.1", proxy_port=8888  # localhost
    )

    # The client object is used to interact with your Azure IoT hub.
    device_client = IoTHubDeviceClient.create_from_connection_string(
        conn_str, websockets=True, proxy_options=proxy_opts
    )

    # Connect the client.
    await device_client.connect()

    async def send_test_message(i):
        print("sending message #" + str(i))
        msg = Message("test wind speed " + str(i))
        msg.message_id = uuid.uuid4()
        msg.correlation_id = "correlation-1234"
        msg.custom_properties["tornado-warning"] = "yes"
        await device_client.send_message(msg)
        print("done sending message #" + str(i))

    # send `messages_to_send` messages in parallel
    await asyncio.gather(*[send_test_message(i) for i in range(1, messages_to_send + 1)])

    # finally, disconnect
    await device_client.disconnect() 
Example #21
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def socket_SOCKS5_IP_test():
    s = socks.socksocket()
    s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
    s.connect(("133.242.129.236", 80))
    s.sendall(raw_HTTP_request())
    status = s.recv(2048).splitlines()[0]
    assert status.startswith(b"HTTP/1.1 200") 
Example #22
Source File: shared_client_tests.py    From azure-iot-sdk-python with MIT License 5 votes vote down vote up
def test_proxy_options(self, client_create_method, create_method_args, mock_pipeline_init):
        proxy_options = ProxyOptions(proxy_type=socks.HTTP, proxy_addr="127.0.0.1", proxy_port=8888)
        client_create_method(*create_method_args, proxy_options=proxy_options)

        # Get configuration object
        assert mock_pipeline_init.call_count == 1
        config = mock_pipeline_init.call_args[0][0]
        assert isinstance(config, ProvisioningPipelineConfig)

        assert config.proxy_options is proxy_options 
Example #23
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def raw_HTTP_request():
    req = "GET /ip HTTP/1.1\r\n"
    req += "Host: ifconfig.me\r\n"
    req += "User-Agent: Mozilla\r\n"
    req += "Accept: text/html\r\n"
    req += "\r\n"
    return req.encode() 
Example #24
Source File: _http.py    From deepWordBug with Apache License 2.0 5 votes vote down vote up
def _open_proxied_socket(url, options, proxy):
    hostname, port, resource, is_secure = parse_url(url)

    if not HAS_PYSOCKS:
        raise WebSocketException("PySocks module not found.")

    ptype = socks.SOCKS5
    rdns = False
    if proxy.type == "socks4":
        ptype = socks.SOCKS4
    if proxy.type == "http":
        ptype = socks.HTTP
    if proxy.type[-1] == "h":
        rdns = True

    sock = socks.create_connection(
            (hostname, port),
            proxy_type = ptype,
            proxy_addr = proxy.host,
            proxy_port = proxy.port,
            proxy_rdns = rdns,
            proxy_username = proxy.auth[0] if proxy.auth else None,
            proxy_password = proxy.auth[1] if proxy.auth else None,
            timeout = options.timeout,
            socket_options = DEFAULT_SOCKET_OPTION + options.sockopt
    )

    if is_secure:
        if HAVE_SSL:
            sock = _ssl_socket(sock, options.sslopt, hostname)
        else:
            raise WebSocketException("SSL not available.")

    return sock, (hostname, port, resource) 
Example #25
Source File: test_utils.py    From telegram-export with Mozilla Public License 2.0 5 votes vote down vote up
def test_parse_proxy_str(self):
        host = "127.0.0.1"
        port = 1080
        
        proxy = (socks.SOCKS5, host, port)
        proxy_str = "socks5://127.0.0.1:1080"
        self.assertEqual(parse_proxy_str(proxy_str), proxy)

        proxy_str = "http://127.0.0.1:1080"
        proxy = (socks.HTTP, host, port)
        self.assertEqual(parse_proxy_str(proxy_str), proxy)

        proxy_str = "socks4://login:password@127.0.0.1:1080"
        proxy = (socks.SOCKS4, host, port, True, "login", "password")
        self.assertEqual(parse_proxy_str(proxy_str), proxy)

        proxy_str = "bad_type://login:password@127.0.0.1:1080"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str)

        proxy_str = "bad_type://127.0.0.1"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str)

        proxy_str = "bad_type:127.0.0.1"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str)

        proxy_str = "127.0.0.1:1080"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str) 
Example #26
Source File: utils.py    From telegram-export with Mozilla Public License 2.0 5 votes vote down vote up
def parse_proxy_str(proxy_str):
    """
    Returns proxy from given string
    """
    if socks is None:
        raise Exception('Please install PySocks if you want to use a proxy')
    url_parser = urlparse(proxy_str)
    proxy_type = None
    proxy_type_str = url_parser.scheme
    
    if proxy_type_str.lower() == "socks5":
        proxy_type = socks.SOCKS5
    elif proxy_type_str.lower() == "socks4":
        proxy_type = socks.SOCKS4
    elif proxy_type_str.lower() == "https":
        proxy_type = socks.HTTP
    elif proxy_type_str.lower() == "http":
        proxy_type = socks.HTTP
    else:
        raise ValueError("Proxy type %s is not supported" % proxy_type)

    host = url_parser.hostname
    port = url_parser.port

    if host is None:
        raise ValueError("Host parsing error")
    if port is None:
        raise ValueError("Port parsing error")

    user = url_parser.username
    password = url_parser.password

    if user is not None and password is not None:
        proxy = (proxy_type, host, port, True, user, password)
    else:
        proxy = (proxy_type, host, port)
    return proxy 
Example #27
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def urllib2_HTTP_test():
    socks.set_default_proxy(socks.HTTP, "127.0.0.1", 8081)
    socks.wrap_module(urllib2)
    status = urllib2.urlopen("http://ifconfig.me/ip").getcode()
    assert status == 200 
Example #28
Source File: _http.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _open_proxied_socket(url, options, proxy):
    hostname, port, resource, is_secure = parse_url(url)

    if not HAS_PYSOCKS:
        raise WebSocketException("PySocks module not found.")

    ptype = socks.SOCKS5
    rdns = False
    if proxy.type == "socks4":
        ptype = socks.SOCKS4
    if proxy.type == "http":
        ptype = socks.HTTP
    if proxy.type[-1] == "h":
        rdns = True

    sock = socks.create_connection(
            (hostname, port),
            proxy_type = ptype,
            proxy_addr = proxy.host,
            proxy_port = proxy.port,
            proxy_rdns = rdns,
            proxy_username = proxy.auth[0] if proxy.auth else None,
            proxy_password = proxy.auth[1] if proxy.auth else None,
            timeout = options.timeout,
            socket_options = DEFAULT_SOCKET_OPTION + options.sockopt
    )

    if is_secure:
        if HAVE_SSL:
            sock = _ssl_socket(sock, options.sslopt, hostname)
        else:
            raise WebSocketException("SSL not available.")

    return sock, (hostname, port, resource) 
Example #29
Source File: sockstest.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def urllib2_handler_HTTP_test():
    opener = urllib2.build_opener(sockshandler.SocksiPyHandler(socks.HTTP, "127.0.0.1", 8081))
    status = opener.open("http://ifconfig.me/ip").getcode()
    assert status == 200 
Example #30
Source File: stomp_transport.py    From resilient-python-api with MIT License 4 votes vote down vote up
def connect(self, timeout=None):
        """ Allow older versions of ssl module, allow http proxy connections """
        LOG.debug("stomp_transport.connect()")
        ssl_params = None
        if isinstance(self.sslContext, dict):
            # This is actually a dictionary of ssl parameters for wrapping the socket
            ssl_params = self.sslContext
            self.sslContext = None

        try:
            if self.proxy_host:
                LOG.info("Connecting through proxy %s", self.proxy_host)
                import socks
                self._socket = socks.socksocket()
                self._socket.set_proxy(socks.HTTP, self.proxy_host, self.proxy_port, True,
                                       username=self.proxy_user, password=self.proxy_password)
            else:
                self._socket = socket.socket()

            self._socket.settimeout(timeout)
            self._socket.connect((self.host, self.port))

            if ssl_params:
                # For cases where we don't have a modern SSLContext (so no SNI)
                cert_required = ssl.CERT_REQUIRED if ssl_params["ca_certs"] else ssl.CERT_NONE
                self._socket = ssl.wrap_socket(
                    self._socket,
                    keyfile=ssl_params['key_file'],
                    certfile=ssl_params['cert_file'],
                    cert_reqs=cert_required,
                    ca_certs=ssl_params['ca_certs'],
                    ssl_version=ssl_params['ssl_version'])
                if cert_required:
                    LOG.info("Performing manual hostname check")
                    cert = self._socket.getpeercert()
                    self.match_hostname(cert, self.host)

            if self.sslContext:
                self._socket = self.sslContext.wrap_socket(self._socket, server_hostname=self.host)

        except IOError as e:
            raise StompConnectionError('Could not establish connection [%s]' % e)
        self._parser.reset()