2023년 11월 29일 수요일

python tornado 업로드 파일 사이즈.

용량이 큰 파일 업로드 시

net::ERR_CONNECTION_RESET 오류 발생할 경우.

http_server = tornado.httpserver.HTTPServer(app, max_buffer_size=10485760000)


upload_form.html

<!DOCTYPE html>
<html>
<head>
<title>File Upload Form</title>
</head>
<body>
<h2>File Upload Form</h2>
<form action="/" method="post" enctype="multipart/form-data">
<label for="file">Select a file:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" value="Upload">
</form>
</body>
</html>

main.py

import os
import tornado.web
import tornado.ioloop

UPLOAD_DIR = "uploads" # Define the directory where uploaded files will be saved


class FileUploadHandler(tornado.web.RequestHandler):
def get(self):
self.render("upload_form.html") # Render the HTML form for file uploads

def post(self):
file_info = self.request.files['file'][0] # Get file information from the request

# Extract relevant file information
file_name = file_info['filename']
file_body = file_info['body']

# Save the uploaded file to the specified directory
file_path = os.path.join(UPLOAD_DIR, file_name)
with open(file_path, 'wb') as file:
file.write(file_body)

self.write(f"File '{file_name}' uploaded successfully.")


def make_app():
return tornado.web.Application([
(r"/", FileUploadHandler),
])


if __name__ == "__main__":
app = make_app()
print("Server is running on http://localhost:8888")
http_server = tornado.httpserver.HTTPServer(app, max_buffer_size=10485760000)
http_server.listen(8888)
tornado.ioloop.IOLoop.current().start()

2023년 11월 22일 수요일

Python을 Windows service로 실행하기

pywin32 설치

python -m pip install --upgrade pywin32 

- 가상환경에서 설치하면 안됨.


가상 환경 외부에서는 COM 객체, 서비스 등을 설치하고자 할 때

python Script/pywin32_postinstall.py -install


[myservice.py]

import win32serviceutil
import ctypes
import time

OutputDebugString = ctypes.windll.kernel32.OutputDebugStringW


class MyService(win32serviceutil.ServiceFramework):
_svc_name_ = 'MyService'
_svc_display_name_ = 'My Service'
is_running = False

def SvcStop(self):
OutputDebugString("MyService __SvcStop__")
self.is_running = False

def SvcDoRun(self):
self.is_running = True
while self.is_running:
OutputDebugString("MyService __loop__")
time.sleep(1)


if '__main__' == __name__:
win32serviceutil.HandleCommandLine(MyService)


관리자 권한으로 PowerSell 실행

python myservice.py install

python myservice.py start

python myservice.py stop

[python] subprocess

proc.py import asyncio from argparse import ArgumentParser from asyncio import sleep async def process_function(user, file_path):     if use...