0

I want to use socks5 proxy in my code. How to do this?

My code:

from urllib.request import Request, urlopen
url = "https://api.ipify.org/"
request = Request(url)
response = urlopen(request)

print(response.read())

In addition I can say that with http/https proxies
I did: request.set_proxy(proxy_address, "http") before
response = urlopen(url), but now it doesn't work.

EDIT
I found sth like that

from urllib.request import Request, urlopen
import socks
import socket

request = Request("https://api.ipify.org/")
socks.set_default_proxy(socks.SOCKS5, IP_ADDR, PORT)
socket.socket = socks.socksocket
response = urlopen(request)

print(response.read())

But it gives me
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain

DeepVisioner
  • 21
  • 1
  • 5

1 Answers1

1

you need to use urllib.request.urlopen with custom ssl context: link here

import socks
import socket
import ssl
from urllib.request import Request, urlopen

IP_ADDR = '127.0.0.1'
PORT = 9050
url = "http://httpbin.org/ip"

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

request = Request(url)
socks.set_default_proxy(socks.SOCKS5, IP_ADDR, PORT)
socket.socket = socks.socksocket
response = urlopen(request, context=ctx)

print(response.read())
isnvi23h4
  • 1,910
  • 1
  • 27
  • 45