title: Python socket, with proxy?
body: <p>I'm developing a function to check the ssl certificate of applications that we use. I developed the following function that is working, but I need it to work using a proxy server, because on the server where the app will be hosted I don't have direct connectivity to all the links I need to monitor. Would anyone know how I can do this connection created via socket go through a proxy?</p>
  <pre><code>import validators
  from urllib.request import ssl, socket
  from datetime import datetime
  import logging
  
  def verify_ssl(url):
      data_ssl = []
      dicio = {}
      try:
          test_url = validators.url(&quot;https://&quot;+url)
          if test_url is True:
              hostname = url
              context = ssl.create_default_context()
  
              
              with socket.create_connection((hostname, &quot;443&quot;)) as sock:
                  with context.wrap_socket(sock, server_hostname = hostname) as ssock:
                      certificate = ssock.getpeercert()
              
                  dicio [&quot;url&quot;] = hostname
                  date = datetime.strptime(certificate[&quot;notAfter&quot;], '%b %d %H:%M:%S %Y %Z')
                  dicio [&quot;notAfter&quot;] = date.strftime(&quot;%d/%m/%Y&quot;)
                  subjectAltName = certificate[&quot;subjectAltName&quot;]
                  y = 0
                  san = []
                  while y &lt; len(subjectAltName):
                      san.append((subjectAltName[y][1]))
                      y += 1 
                  dicio [&quot;subjectAltName&quot;] = san
                  dicio [&quot;issuer&quot;] = dict((y,z) for y,z in certificate[&quot;issuer&quot;][len(certificate [&quot;issuer&quot;])-1])['commonName']
                  dicio [&quot;local&quot;] = 'Manual'
                  # dicio [&quot;cadastrada&quot;] = checkUrl(url,'prod')
              data_ssl.append(dicio)
            
          else:
              logging.error(f'{url}')
              dicio [&quot;url&quot;] = url
              dicio [&quot;notAfter&quot;] = 'Error'
              dicio [&quot;subjectAltName&quot;] = 'Error'
              dicio [&quot;issuer&quot;] = 'Error'
              dicio [&quot;local&quot;] = 'Manual'
              dicio [&quot;cadastrada&quot;] = 'Error'
              data_ssl.append(dicio)
  
              
      except Exception as e:
          logging.error(f'{url} - {e}')
          dicio [&quot;url&quot;] = url
          dicio [&quot;notAfter&quot;] = 'Error'
          dicio [&quot;subjectAltName&quot;] = 'Error'
          dicio [&quot;issuer&quot;] = 'Error'
          dicio [&quot;local&quot;] = 'Manual'
          dicio [&quot;cadastrada&quot;] = 'Error'
          data_ssl.append(dicio)
      return data_ssl
  </code></pre>