programing

Python, 기본 인증을 사용하는 HTTPS GET

stoneblock 2023. 7. 13. 20:22

Python, 기본 인증을 사용하는 HTTPS GET

저는 파이썬을 이용한 기본 인증으로 HTTPS GET를 하려고 합니다.나는 파이썬에 매우 익숙하지 않고 가이드들은 다른 라이브러리를 사용하여 일을 하는 것 같습니다.(https.client, httplib 및 urllib).누가 어떻게 하는지 보여줄 수 있나요?표준 라이브러리를 사용하도록 어떻게 지시할 수 있습니까?

Python 3에서는 다음이 작동합니다.표준 라이브러리의 하위 수준 http.client를 사용하고 있습니다.또한 기본 승인에 대한 자세한 내용은 rfc2617의 섹션 2를 참조하십시오.이 코드는 인증서가 유효한지 확인하지 않지만 https 연결을 설정합니다.방법은 http.client 문서를 참조하십시오.

from http.client import HTTPSConnection
from base64 import b64encode


# Authorization token: we need to base 64 encode it 
# and then decode it to acsii as python 3 stores it as a byte string
def basic_auth(username, password):
    token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii")
    return f'Basic {token}'

username = "user_name"
password = "password"

#This sets up the https connection
c = HTTPSConnection("www.google.com")
#then connect
headers = { 'Authorization' : basic_auth(username, password) }
c.request('GET', '/', headers=headers)
#get the response back
res = c.getresponse()
# at this point you could check the status etc
# this gets the page text
data = res.read()  

Python의 강력한 기능을 사용하여 최고의 라이브러리 중 하나에 기대십시오.

import requests

r = requests.get('https://my.website.com/rest/path', auth=('myusername', 'mybasicpass'))
print(r.text)

변수 r(응답 요청)에는 사용할 수 있는 모수가 훨씬 많습니다.가장 좋은 방법은 인터렉티브 인터프리터에 들어가서 그것을 가지고 놀거나 요청 문서를 읽는 것입니다.

ubuntu@hostname:/home/ubuntu$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> r = requests.get('https://my.website.com/rest/path', auth=('myusername', 'mybasicpass'))
>>> dir(r)
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
>>> r.content
b'{"battery_status":0,"margin_status":0,"timestamp_status":null,"req_status":0}'
>>> r.text
'{"battery_status":0,"margin_status":0,"timestamp_status":null,"req_status":0}'
>>> r.status_code
200
>>> r.headers
CaseInsensitiveDict({'x-powered-by': 'Express', 'content-length': '77', 'date': 'Fri, 20 May 2016 02:06:18 GMT', 'server': 'nginx/1.6.3', 'connection': 'keep-alive', 'content-type': 'application/json; charset=utf-8'})

업데이트: OP는 Python 3을 사용합니다.그래서 httplib2를 사용하여 예제를 추가합니다.

import httplib2

h = httplib2.Http(".cache")

h.add_credentials('name', 'password') # Basic authentication

resp, content = h.request("https://host/path/to/resource", "POST", body="foobar")

다음은 python 2.6에서 작동합니다.

사용합니다pycurl하루에 천만 건 이상의 요청을 처리하는 프로세스를 위해 많은 생산 작업을 수행하고 있습니다.

먼저 다음 항목을 가져와야 합니다.

import pycurl
import cStringIO
import base64

기본 인증 헤더의 일부는 Base64로 인코딩된 사용자 이름과 암호로 구성됩니다.

headers = { 'Authorization' : 'Basic %s' % base64.b64encode("username:password") }

HTTP 헤더에 이 행이 표시됩니다.Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=인코딩된 문자열은 사용자 이름과 암호에 따라 변경됩니다.

우리는 이제 HTTP 응답을 쓸 장소와 컬 연결 핸들이 필요합니다.

response = cStringIO.StringIO()
conn = pycurl.Curl()

다양한 컬 옵션을 설정할 수 있습니다.전체 옵션 목록은 다음을 참조하십시오.링크된 설명서는 libcurl API용이지만 다른 언어 바인딩에 대한 옵션은 변경되지 않습니다.

conn.setopt(pycurl.VERBOSE, 1)
conn.setopt(pycurlHTTPHEADER, ["%s: %s" % t for t in headers.items()])

conn.setopt(pycurl.URL, "https://host/path/to/resource")
conn.setopt(pycurl.POST, 1)

인증서를 확인할 필요가 없는 경우.경고:이것은 안전하지 않습니다.달리는 것과 유사함curl -k또는curl --insecure.

conn.setopt(pycurl.SSL_VERIFYPEER, False)
conn.setopt(pycurl.SSL_VERIFYHOST, False)

불러cStringIO.writeHTTP 응답을 저장합니다.

conn.setopt(pycurl.WRITEFUNCTION, response.write)

POST 요청을 할 때.

post_body = "foobar"
conn.setopt(pycurl.POSTFIELDS, post_body)

지금 실제 요청을 하십시오.

conn.perform()

HTTP 응답 코드를 기반으로 작업을 수행합니다.

http_code = conn.getinfo(pycurl.HTTP_CODE)
if http_code is 200:
   print response.getvalue()

Python3에서 인증서 유효성 검사를 통해 기본 인증을 수행하는 올바른 방법은 다음과 같습니다.

필수 사항은 아닙니다.OS 번들(*nix만 해당)을 사용하거나 Mozilla의 CA 번들을 직접 배포할 수 있습니다.또는 통신하는 호스트가 몇 개에 불과한 경우 호스트의 CA에서 직접 CA 파일을 연결하여 다른 손상된 CA로 인한 MitM 공격 위험을 줄일 수 있습니다.

#!/usr/bin/env python3


import urllib.request
import ssl

import certifi


context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations(certifi.where())
httpsHandler = urllib.request.HTTPSHandler(context = context)

manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, 'https://domain.com/', 'username', 'password')
authHandler = urllib.request.HTTPBasicAuthHandler(manager)

opener = urllib.request.build_opener(httpsHandler, authHandler)

# Used globally for all urllib.request requests.
# If it doesn't fit your design, use opener directly.
urllib.request.install_opener(opener)

response = urllib.request.urlopen('https://domain.com/some/path')
print(response.read())

@AndrewCox의 몇 가지 사소한 개선 사항에 대한 답변을 기반으로 합니다.

from http.client import HTTPSConnection
from base64 import b64encode


client = HTTPSConnection("www.google.com")
user = "user_name"
password = "password"
headers = {
    "Authorization": "Basic {}".format(
        b64encode(bytes(f"{user}:{password}", "utf-8")).decode("ascii")
    )
}
client.request('GET', '/', headers=headers)
res = client.getresponse()
data = res.read()

참고로, 다음을 사용하는 경우 인코딩을 설정해야 합니다.bytes 니다합능대 대신 합니다.b"".

requests.get(url, auth=requests.auth.HTTPBasicAuth(username=token, password=''))

토큰을 사용하는 경우 암호는 다음과 같아야 합니다.''.

저한테는 효과가 있어요.

표준 모듈만 사용하고 수동 헤더 인코딩은 사용하지 않음

...이것이 의도되고 가장 휴대하기 쉬운 방법인 것 같습니다.

python urllib의 개념은 요청의 다양한 속성을 다양한 관리자/관리자/관리자로 그룹화하는 것입니다.그리고 나서 그들의 부품을 처리합니다.

import urllib.request, ssl

# to avoid verifying ssl certificates
httpsHa = urllib.request.HTTPSHandler(context= ssl._create_unverified_context())

# setting up realm+urls+user-password auth
# (top_level_url may be sequence, also the complete url, realm None is default)
top_level_url = 'https://ip:port_or_domain'
# of the std managers, this can send user+passwd in one go,
# not after HTTP req->401 sequence
password_mgr = urllib.request.HTTPPasswordMgrWithPriorAuth()
password_mgr.add_password(None, top_level_url, "user", "password", is_authenticated=True)

handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
# create OpenerDirector
opener = urllib.request.build_opener(handler, httpsHa)

url = top_level_url + '/some_url?some_query...'
response = opener.open(url)

print(response.read())

GET & POST 요청은 일반적으로 양식을 제출하는 데 사용됩니다.다음은 사용법에 대한 간단한 예입니다.

Views.py

def index(request)
    col1 = float(request.GET.get('col1'))

색인.

<div class="form-group col-md-2">
        <label for="Col 1">Price</label>
        <input type="number" class="form-control" id="col1" name="col1">
    </div>

언급URL : https://stackoverflow.com/questions/6999565/python-https-get-with-basic-authentication