Python

Python) 파이썬 URL, Base64 인코딩 디코딩

로픽 2022. 1. 14. 16:37
300x250

파이썬 URL, Base64 인코딩 디코딩

 

파이썬 URL 및 Base64 인코딩/디코딩 방법

 

* 핵심코드

# URL 인/디코딩
from urllib import parse

incoding_url = parse.quote("초기문자열")		# URL Incoding
decoding_url = parse.unquote("인코딩문자열")	# URL Decoding


# Base64 인/디코딩
import base64

# Base64 Incoding
str_bytes = "초기문자열".encode('utf-8')
str_base64 = base64.b64encode(str_bytes)
base64_str = str_base64.decode('utf-8')

# Base64 Decoding
base64_str = "인코딩문자열"
str_bytes = base64.b64decode(base64_str)
init_str = str_bytes.decode('utf-8')

* 콘솔 출력

from urllib import parse
import base64

def url_Incoding(init_str):
    incoding_url = parse.quote(init_str)
    print(incoding_url)
    return incoding_url

def url_Decoding(incode_str):
    decoding_url = parse.unquote(incode_str)
    print(decoding_url)
    return decoding_url

def base64_Incoding(init_str):
    str_bytes = init_str.encode('utf-8')
    str_base64 = base64.b64encode(str_bytes)
    base64_str = str_base64.decode('utf-8')
    print(base64_str)
    return base64_str

def base64_Decoding(incode_str):
    base64_str = incode_str
    str_bytes = base64.b64decode(base64_str)
    init_str = str_bytes.decode('utf-8')
    print(init_str)
    return init_str

if __name__ == '__main__':
    # Url Incoding/Decoding
    url_text = url_Incoding('https://www.naver.com/')
    url_Decoding(url_text)

    #Base64 Incoding/Decoding
    init_text = base64_Incoding('PyCharm')
    base64_Decoding(init_text)

* 콘솔 결과

 

 


* 22.02.01 수정

  - 한글 base64 인코딩시 에러 발생. ( .encode('acssii') > .encode('utf-8') 변경 )

 

 

 

반응형