Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 11개 있습니다.)
개발 환경 구성: 592. JetBrains의 IDE(예를 들어, PyCharm)에서 Visual Studio 키보드 매핑 적용
; https://www.sysnet.pe.kr/2/0/12752

개발 환경 구성: 595. PyCharm - WSL과 연동해 Django App을 윈도우에서 리눅스 대상으로 개발
; https://www.sysnet.pe.kr/2/0/12773

개발 환경 구성: 597. PyCharm - 윈도우 환경에서 WSL을 이용해 파이썬 앱 개발/디버깅하는 방법
; https://www.sysnet.pe.kr/2/0/12789

개발 환경 구성: 598. PyCharm - 원격 프로세스를 디버그하는 방법
; https://www.sysnet.pe.kr/2/0/12798

개발 환경 구성: 599. PyCharm - (반대로) 원격 프로세스가 PyCharm에 디버그 연결하는 방법
; https://www.sysnet.pe.kr/2/0/12800

개발 환경 구성: 601. PyCharm - 다중 프로세스 디버깅 방법
; https://www.sysnet.pe.kr/2/0/12806

스크립트: 26. 파이썬 - PyCharm을 이용한 fork 디버그 방법
; https://www.sysnet.pe.kr/2/0/12823

개발 환경 구성: 603. GoLand - WSL 환경과 연동
; https://www.sysnet.pe.kr/2/0/12825

개발 환경 구성: 673. JetBrains IDE에서 "Squash Commits..." 메뉴가 비활성화된 경우
; https://www.sysnet.pe.kr/2/0/13318

개발 환경 구성: 679. PyCharm(을 비롯해 JetBrains에 속한 여타) IDE에서 내부 Window들의 탭이 없어진 경우
; https://www.sysnet.pe.kr/2/0/13369

개발 환경 구성: 697. GoLand에서 ldflags 지정 방법
; https://www.sysnet.pe.kr/2/0/13489




PyCharm - (반대로) 원격 프로세스가 PyCharm에 디버그 연결하는 방법

일반적으로 비주얼 스튜디오에 익숙한 사용자라면 원격 디버깅이라고 할 때 이전에 설명했던,

PyCharm - 원격 프로세스를 디버그하는 방법
; https://www.sysnet.pe.kr/2/0/12798

방식의 디버깅만을 연상하게 됩니다. 그런데, 특이하게도 PyCharm의 경우 프로세스 측에서 PyCharm IDE로 디버그 연결을 하는 방법을 함께 제공합니다.

이런 문제는, 아마도 지난 글에 소개한,

파이썬 - WSGI를 만족하는 최소한의 구현 코드 및 PyCharm에서의 디버깅 방법
; https://www.sysnet.pe.kr/2/0/12794

simple_app과 같은 앱을 uwsgi/gunicorn 등에서 호스팅할 때 디버깅을 원한다면 사용할 수 있는 대안으로써 제공되는 듯합니다. 이와 관련된 공식 문서도 있지만,

debugging a uwsgi python application using pycharm
; https://stackoverflow.com/questions/21257568/debugging-a-uwsgi-python-application-using-pycharm

Remote debugging with the Python remote debug server configuration
; https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config

역시 문서보다 ^^ 직접 상황에 따른 예를 드는 것이 더 이해가 빠르겠지요. 이번에도 "PyCharm - 원격 프로세스를 디버그하는 방법"에서처럼 원격 PC가 있어야겠지만 어차피 설정은 모두 동일하므로 WSL 환경에서 테스트를 하겠습니다.

즉, 원격 PC(또는 WSL)에서 실행한 uwsgi/gunicorn 프로세스에서 호스팅되는 Python App을 디버깅하는 것을 가정하고 설명하겠습니다. ^^




우선, PyCharm IDE에서 새로운 "Run/Debug Configuration"을 "Python Debug Server" 유형으로,

passive_remote_debug_1.png

포트와 IDE host name을 입력해 생성합니다. (이 글에서는 WSL 환경이므로 localhost와 임의의 포트를 지정했습니다. 일반적인 상황이라면, "0.0.0.0"으로 AnyIP로 설정하는 것이 속 편합니다.)

그런 다음, 화면에 나오는 텍스트에서 지시하는 것처럼, 원격 PC(또는 WSL) 측에서 "pydevd-pycharm~=212.4746.96" 구성 요소를 설치하고,

$ pip install pydevd-pycharm~=212.4746.96

원격 PC에서 실행할 스크립트에는 적절한 디버그 시점을 선택해 아래의 스크립트를 추가합니다. (즉, 아래의 경우는 시작하자마자 PyCharm IDE의 디버그 서버로 접속을 시도합니다.)

# main.py

import pydevd_pycharm
pydevd_pycharm.settrace('localhost', port=14444, stdoutToServer=True, stderrToServer=True)


HELLO_WORLD = b"Hello world!\n"


def simple_app(environ, start_response):
    """Simplest possible application object"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return [HELLO_WORLD]


def get_wsgi_application():
    return simple_app


application = get_wsgi_application()

자, 이걸로 준비 작업은 끝입니다. 이제 PyCharm은 해당 Configuration의 설정으로 디버그를 시작하면 콘솔 화면에 "Waiting for process connection..."이라고 뜨며 연결을 대기합니다.

이후, WSL 측에서 파이썬 스크립트를 실행하면,

$ python main.py
Could not connect to localhost: 14444
Traceback (most recent call last):
  File "/home/testusr/.local/lib/python3.8/site-packages/_pydevd_bundle/pydevd_comm.py", line 507, in start_client
    s.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    pydevd.settrace('localhost', port=14444, stdoutToServer=True, stderrToServer=True)
  File "/home/testusr/.local/lib/python3.8/site-packages/pydevd.py", line 2696, in settrace
    _locked_settrace(
  File "/home/testusr/.local/lib/python3.8/site-packages/pydevd.py", line 2777, in _locked_settrace
    py_db.connect(host, port)  # Note: connect can raise error.
  File "/home/testusr/.local/lib/python3.8/site-packages/pydevd.py", line 1353, in connect
    s = start_client(host, port)
  File "/home/testusr/.local/lib/python3.8/site-packages/_pydevd_bundle/pydevd_comm.py", line 507, in start_client
    s.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused

(원격 호스트라면 오류 없이 잘 실행이 되었겠지만) 아쉽게도 WSL 환경이라 "Could not connect to localhost - ConnectionRefusedError: [Errno 111] Connection refused" 오류가 발생하는군요. ^^




이에 대한 원인은 WSL 2에서의 네트워크의 특징 때문입니다.

WSL 2의 네트워크 통신 방법
; https://www.sysnet.pe.kr/2/0/12347

즉, WSL 인스턴스 측에서 윈도우 호스트 측으로의 "localhost" 접속이 불가능하므로, PyCharm 측의 "IDE host name" 바인딩을 "localhost"가 아닌 "/etc/resolv.conf"에서 보이는 IP로,

$  cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.23.208.1

대체해야 합니다. (혹은 그냥 위에서도 언급했지만 "0.0.0.0"으로 설정합니다.) 물론, 스크립트에서도 변경해주고,

pydevd_pycharm.settrace('172.23.208.1', port=14444, stdoutToServer=True, stderrToServer=True)

다시 실행하면 이번에는 정상적으로 디버깅 연결이 됩니다.




그런데, 디버깅 연결까지는 되었지만 아마도 화면에는 여전히 다음과 같은 식의 오류가 발생할 수 있습니다.

/mnt/d/workshop2/python-wsgi-app/main.py can't be found in project. You can continue debugging, but without the source. To fix that you can do one of the following:


소스 코드를 찾을 수 없다는 것인데, 어쩔 수 없습니다. "Run/Debug Configuration"에서 다음과 같이 매핑을 추가해야 합니다.

passive_remote_debug_2.png

이후 다시 디버깅 연결을 하면 콘솔에는 다음의 메시지와 함께,

Starting debug server at port 14,444
Use the following code to connect to the debugger:
import pydevd_pycharm
pydevd_pycharm.settrace('172.23.208.1', port=14444, stdoutToServer=True, stderrToServer=True)
Waiting for process connection…
Connected to pydev debugger (build 212.4746.96)

정상적으로 main.py가 열리고 pydevd.settrace 호출 다음 라인에서 실행이 멈춰 있는 것을 확인할 수 있습니다.

참고로 PyCharm IDE는 디버깅 서버를 한 번 열어 두면 계속 접속을 받아들이도록 동작합니다. 따라서, 클라이언트 측에서 프로세스를 종료해 연결이 끊겨도 PyCharm 측의 디버그 서버를 다시 실행할 필요 없이 그냥 클라이언트 측 프로세스만 다시 시작해 pydevd.settrace 호출을 하면 PyCharm 디버그 서버에 연결이 됩니다.




이로 인해 다음의 소스 코드를,

import pydevd
pydevd.settrace('172.23.208.1', port=14444, stdoutToServer=True, stderrToServer=True)

HELLO_WORLD = b"Hello world!\n"


def simple_app(environ, start_response):
    """Simplest possible application object"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return [HELLO_WORLD]


def get_wsgi_application():
    return simple_app


application = get_wsgi_application()

이전에는 gunicorn으로 실행하는 경우,

$ gunicorn -b 0.0.0.0:18090 main

PyCharm에서 (F5 실행) 디버깅을 할 수 없었지만 이제는 pydevd.settrace의 호출로 인해 디버깅이 가능해졌습니다.

(그건 그렇고, 이건 Visual Studio에도 도입을 했으면... 하는 희망이 생기는군요. ^^)




참고로, 아래와 같이 코딩해도 되지만,

import pydevd_pycharm
pydevd_pycharm.settrace('172.23.208.1', port=14444, stdoutToServer=True, stderrToServer=True)

이렇게 해도 정상적으로 동작을 했습니다.

import pydevd
pydevd.settrace('172.23.208.1', port=14444, stdoutToServer=True, stderrToServer=True)

이에 대한 이력은 잘 모르겠지만 pydevd는 예전 모듈인 것 같고, 하위 호한으로 인해 남겨둔 듯합니다. (혹시 이에 대한 차이를 아시는 분은 덧글 부탁드립니다. ^^)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 8/22/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13501정성태12/25/20232086개발 환경 구성: 700. WSL + uwsgi - IPv6로 바인딩하는 방법
13500정성태12/24/20232178디버깅 기술: 194. Windbg - x64 가상 주소를 물리 주소로 변환
13498정성태12/23/20232821닷넷: 2186. 한국투자증권 KIS Developers OpenAPI의 C# 래퍼 버전 - eFriendOpenAPI NuGet 패키지
13497정성태12/22/20232294오류 유형: 885. Visual Studiio - error : Could not connect to the remote system. Please verify your connection settings, and that your machine is on the network and reachable.
13496정성태12/21/20232307Linux: 66. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (gdb)
13495정성태12/20/20232317Linux: 65. clang++로 공유 라이브러리의 -static 옵션 빌드가 가능할까요?
13494정성태12/20/20232500Linux: 64. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency) - 두 번째 이야기
13493정성태12/19/20232546닷넷: 2185. C# - object를 QueryString으로 직렬화하는 방법
13492정성태12/19/20232261개발 환경 구성: 699. WSL에 nopCommerce 예제 구성
13491정성태12/19/20232232Linux: 63. 리눅스 - 다중 그룹 또는 사용자를 리소스에 권한 부여
13490정성태12/19/20232345개발 환경 구성: 698. Golang - GLIBC 의존을 없애는 정적 빌드 방법
13489정성태12/19/20232133개발 환경 구성: 697. GoLand에서 ldflags 지정 방법
13488정성태12/18/20232067오류 유형: 884. HTTP 500.0 - 명령행에서 실행한 ASP.NET Core 응용 프로그램을 실행하는 방법
13487정성태12/16/20232381개발 환경 구성: 696. C# - 리눅스용 AOT 빌드를 docker에서 수행 [1]
13486정성태12/15/20232194개발 환경 구성: 695. Nuget config 파일에 값 설정/삭제 방법
13485정성태12/15/20232088오류 유형: 883. dotnet build/restore - error : Root element is missing
13484정성태12/14/20232161개발 환경 구성: 694. Windows 디렉터리 경로를 WSL의 /mnt 포맷으로 구하는 방법
13483정성태12/14/20232296닷넷: 2184. C# - 하나의 resource 파일을 여러 프로그램에서 (AOT 시에도) 사용하는 방법파일 다운로드1
13482정성태12/13/20232817닷넷: 2183. C# - eFriend Expert OCX 예제를 .NET Core/5+ Console App에서 사용하는 방법 [2]파일 다운로드1
13481정성태12/13/20232266개발 환경 구성: 693. msbuild - .NET Core/5+ 프로젝트에서 resgen을 이용한 리소스 파일 생성 방법파일 다운로드1
13480정성태12/12/20232602개발 환경 구성: 692. Windows WSL 2 + Chrome 웹 브라우저 설치
13479정성태12/11/20232299개발 환경 구성: 691. WSL 2 (Ubuntu) + nginx 환경 설정
13477정성태12/8/20232480닷넷: 2182. C# - .NET 7부터 추가된 Int128, UInt128 [1]파일 다운로드1
13476정성태12/8/20232206닷넷: 2181. C# - .NET 8 JsonStringEnumConverter의 AOT를 위한 개선파일 다운로드1
13475정성태12/7/20232277닷넷: 2180. .NET 8 - 함수 포인터에 대한 Reflection 정보 조회파일 다운로드1
13474정성태12/6/20232133개발 환경 구성: 690. 닷넷 코어/5+ 버전의 ilasm/ildasm 실행 파일 구하는 방법 - 두 번째 이야기
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...