본문 바로가기
IT/파이썬

파이썬 flask - 접속 IP 어드레스 알아내기

간단하게 코드로 접속된 사용자의 IP address 를 알아낼 수 있다.

아래 글을 참고하시던지, 아래 코드를 참고하세요.

 

ip_address = flask.request.remote_addr

 

www.kite.com/python/answers/how-to-get-an-ip-address-using-flask-in-python

 

Kite - Free AI Coding Assistant and Code Auto-Complete Plugin

Code faster with Kite’s AI-powered autocomplete plugin for over 16 programming languages and 16 IDEs, featuring Multi-Line Completions. Works 100% locally.

www.kite.com

app = flask.Flask(__name__)

@app.route("/")
def index():
    ip_address = flask.request.remote_addr
    return "Requester IP: " + ip_address

app.run(host="0.0.0.0", port=8080)

> 결과 예시
HTTP REQUEST
GET http://localhost:8080/
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
HTTP RESPONSE
HTTP 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 23
Server: Werkzeug/0.16.0 Python/3.7.3
Date: Fri, 27 Mar 2020 19:32:39 GMT

Requester IP: 127.0.0.1

BJ.

숫자 번호 주소 https://stocksnap.io/photo/still-items-RDX59GX2JT