>source

Pysocks를 사용하여 https 요청을 보내는 방법 만 잘 작동합니까?

import socks
s = socks.socksocket()
s.set_proxy(socks.HTTP, "proxy", port)
s.connect(("example.com", 443))
request = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\nSec-Fetch-Site: none\r\nSec-Fetch-Mode: navigate\r\nSec-Fetch-Dest: document\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7\r\n\r\n"
s.sendall(request.encode())
result = s.recv(4096)
while (len(result) > 0):
     print(str(result))
     result = s.recv(1024)
     result

응답

Process finished with exit code 0

  • 답변 # 1

    안전하게 연결하려면 HTTPS 원래 소켓의 래퍼로 ssl이 필요합니다.

    import socks
    import ssl
    s = socks.socksocket()
    #s.set_proxy(socks.HTTP, "proxy", port)  # I don't have proxy server
    s.connect(("httpbin.org", 443))
    ss = ssl.wrap_socket(s, keyfile=None, certfile=None, server_side=False, cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_SSLv23)
    request = "GET /get HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\nSec-Fetch-Site: none\r\nSec-Fetch-Mode: navigate\r\nSec-Fetch-Dest: document\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7\r\n\r\n"
    ss.sendall(request.encode())
    result = ss.recv(4096)
    while result:
         print(result.decode())
         result = ss.recv(1024)
    
    

    모든 데이터를 서버로 보내는 실제 URL https://httpbin.org/get으로 테스트했습니다.

    HTTP/1.1 200 OK
    Date: Fri, 18 Dec 2020 02:23:27 GMT
    Content-Type: application/json
    Content-Length: 733
    Connection: close
    Server: gunicorn/19.9.0
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    
    {
      "args": {}, 
      "headers": {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 
        "Accept-Encoding": "gzip, deflate", 
        "Accept-Language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7", 
        "Host": "example.com", 
        "Sec-Fetch-Dest": "document", 
        "Sec-Fetch-Mode": "navigate", 
        "Sec-Fetch-Site": "none", 
        "Upgrade-Insecure-Requests": "1", 
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36", 
        "X-Amzn-Trace-Id": "Root=1-5fdc129f-055e9976503fef011cc031c0"
      }, 
      "origin": "217.99.86.56", 
      "url": "https://example.com/get"
    }
    
    

    <시간 />

    편집하다:

    로컬 TOR 프록시 서버에서도 작동합니다.

    s.set_proxy(socks.SOCKS5, "localhost", 9050)  # TOR proxy server
    
    

관련 자료

  • 이전 javascript - vuejs 현재 타임 스탬프 이동
  • 다음 마이그레이션 활동을 위해 Oracle SQL을 SQL Server 날짜로 변환하려고합니다