Не получается корректно запустить python скрипт в Django
У меня есть скрипт авторизации телеграмм
auth.py
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
api_id = 'id'
api_hash = 'hash'
username = 'test'
client = TelegramClient(username, api_id, api_hash)
client.start()
print("Client Created")
if not client.is_user_authorized():
client.send_code_request(phone)
try:
client.sign_in(phone, input('Enter the code: '))
except SessionPasswordNeededError:
client.sign_in(password=input('Password: '))
Так же в Django
views.py
def test(request):
if 'activation_key' in request.session:
key = request.session['activation_key']
parm = {'key': key, 'files': file_list}
if request.method == "POST" and 'start' in request.POST:
start = request.POST.get('start', True)
script = (r'C:\Users\kato-pc\Desktop\rad\mysite\c1\auth.py')
print("запущено")
subprocess.run(script, shell=True)
return redirect('/main/test', {'output': start})
if request.method == "POST" and 'command' in request.POST:
input1 = request.POST.get('command', True)
script = (r'C:\Users\kato-pc\Desktop\rad\mysite\c1\auth.py')
try:
check_output(script, input=input1, shell=True, encoding='utf-8',stderr=subprocess.STDOUT, capture_output=False)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
print("нажал")
return redirect('/main/test', {'output1': input1})
return render(request, 'main/test.html', parm)
else:
return redirect('/')
urls.py стандартный
path('test/', views.test, name = 'test')
test.html
<form action = "/main/test/" method = "POST">
{% csrf_token %}
<input id="start" type="text" name="start" />
<input class="io" id="start" type="submit" name="start" value="Start"/>
</form>
----
{{ screen }}
{{ start }}
------
<form action = "/main/test/" method = "POST">
{% csrf_token %}
<input id="command" type="text" name="command" />
<input class="io" id="start1" type="submit" name="start1" value="Send"/>
{{ output1 }}
</form>
Мне, чтобы написать дальнейший код нужно получить выходной файл сессии при завершении скрипта auth.py, но субпроцесс хочет, чтобы я ввёл в инпут поля номер телефона и код одновременно, что невозможно, идея в том, что я одной кнопкой запускаю скрипт, а второй кнопкой передаю введённые данные в этот процесс, но у меня выходит ошибка:
RuntimeError at /main/test/ command 'C:\Users\kato-pc\Desktop\rad\mysite\c1\auth.py' return with error (code 1): Please enter your phone (or bot token): Please enter the code you received: Traceback (most recent call last): File "C:\Users\kato-pc\Desktop\rad\mysite\c1\auth.py", line 13, in client.start() File "C:\Users\kato-pc\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\auth.py", line 133, in start else self.loop.run_until_complete(coro) File "C:\Users\kato-pc\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete return future.result() File "C:\Users\kato-pc\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\auth.py", line 193, in _start value = code_callback() File "C:\Users\kato-pc\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\auth.py", line 107, in code_callback return input('Please enter the code you received: ') EOFError: EOF when reading a line
И главное смска с кодом в телеграмм приходит, но я не могу послать код, подскажите пожалуйста, как реализовать?