파이썬으로 웹 단순 예제. 파이썬 비동기 함수. 파이썬 non-blocking python do_get non-block
작성자운영자작성시간19.05.23조회수279 목록 댓글 0"""
웹 브라우저를 열고 http://127.0.0.1:8606 라고 접속을 하면 되어요
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading, time
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write(bytes("<html><body><h1>안녕하세요. GET방식입니다.</h1></body></html>", "utf-8"))
print("my_crawling_func함수 호출 전")
thread = threading.Thread(target=my_crawling_func, args=("인자1", "인자2"))
thread.start()
print("my_crawling_func함수 호출 후")
def do_HEAD(self):
self._set_headers()
def do_POST(self):
self._set_headers()
self.wfile.write(bytes("<html><body><h1>안녕하세요. POST방식입니다.</h1></body></html>", "utf-8"))
def run(server_class=HTTPServer, handler_class=S, port=8606):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print("웹서버를 시작합니다.")
httpd.serve_forever()
def my_crawling_func(arg, arg2):
print("쓰레드로 실행한 함수입니다. Args:" + arg + ", " + arg2)
print("크롤링 중")
time.sleep(1)
print("크롤링으로 이름가져오는중")
time.sleep(2)
print("크롤링으로 게시글 가져오는 중")
time.sleep(2)
print("크롤링 데이터 적재중")
time.sleep(1)
print("크롤링 완료")
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
다음검색