FinalCommit

This commit is contained in:
R5600U_PC 2024-11-01 21:57:45 +09:00
parent 25ef8f51fa
commit a2125db289
6 changed files with 105 additions and 0 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ pyvenv.cfg
__pycache__/
src/__pycache__/
src/browsers/user_data/
build/

View File

@ -0,0 +1,16 @@
FROM python:3.10-slim
# 작업 디렉토리 설정
WORKDIR /app
# 필요한 파일 복사 및 패키지 설치
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install playwright gunicorn
RUN playwright install --with-deps
# 애플리케이션 코드 복사
COPY . .
# gunicorn 실행 (운영 환경 설정)
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5690", "backend:app"]

View File

@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
from flask import Flask, jsonify
from playwright.sync_api import sync_playwright
import redis
import time
app = Flask(__name__)
r = redis.StrictRedis(host='redis', port=6379, db=0, decode_responses=True)
SESSION_TTL = 3600 # 1시간
def login_and_store_session():
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=True)
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
context = browser.new_context(
user_agent=user_agent,
permissions=["geolocation", "notifications"]
)
page = context.new_page()
# A 사이트 로그인 절차
#page.goto("https://www.percenty.co.kr/signin")
#page.fill('input[placeholder="이메일 주소 입력"]', 'leensoo1nt@gmail.com')
#page.fill('input[placeholder="영문/숫자/특수문자의 조합 (6~15자리)"]', 'gytkd9570!')
#page.click('button:has-text("로그인 하기")')
page.goto('https://www.auction1.co.kr/common/login_box.php')
page.fill('//*[@id="client_id"]', 'thanks9')
page.fill('//*[@id="pw_Dummy"]', 'thanks9!')
page.click('//*[@id="fm_login"]/div[4]/a[1]/span')
# 로그인 후 세션 저장
page.wait_for_load_state('networkidle')
cookies = context.cookies()
# 세션 만료 시간 계산 (예: 현재 시간 + 1시간)
session_expiry_time = time.time() + 3600 # 1시간
# 세션 데이터를 Redis에 저장 (만료 시간 포함)
session_data = {"cookies": cookies, "expires_at": session_expiry_time}
r.setex("session_key", 3600, str(session_data)) # TTL 1시간 설정
browser.close()
return session_data
@app.route('/get-session', methods=['GET'])
def get_session():
session_id = r.get("session_key")
if session_id:
# Redis에서 세션 데이터를 가져와 유효성 확인
session_data = eval(session_id) # eval 사용 시 보안 이슈에 주의
if session_data.get("expires_at") > time.time():
# 세션이 유효한 경우 반환
return jsonify({"session": session_data, "status": "existing"})
else:
# 세션이 만료된 경우 새로운 세션 생성
return jsonify({"session": login_and_store_session(), "status": "renewed"})
else:
# 세션이 없을 경우 로그인 후 반환
return jsonify({"session": login_and_store_session(), "status": "new"})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5690)

View File

@ -0,0 +1,4 @@
flask
playwright
redis
gunicorn

View File

@ -0,0 +1,17 @@
version: '3.8'
services:
backend:
build:
context: ./backend
ports:
- "5690:5690"
volumes:
- ./backend:/app
depends_on:
- redis
redis:
image: "redis:alpine"
ports:
- "6379:6379"

BIN
requirements.txt Normal file

Binary file not shown.