Mercurial > hg > orthanc-book
comparison Sphinx/source/plugins/python.rst @ 697:7581dc208323
Handling DICOM SCP requests using a Python plugin
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Jun 2021 18:36:22 +0200 |
parents | 29e49f03dc27 |
children | b931786385e6 |
comparison
equal
deleted
inserted
replaced
696:29e49f03dc27 | 697:7581dc208323 |
---|---|
64 ----- | 64 ----- |
65 | 65 |
66 Docker | 66 Docker |
67 ...... | 67 ...... |
68 | 68 |
69 .. highlight:: json | 69 .. highlight:: python |
70 | 70 |
71 The most direct way of starting Orthanc together with the Python | 71 The most direct way of starting Orthanc together with the Python |
72 plugin is through :ref:`Docker <docker>`. Let's create the file | 72 plugin is through :ref:`Docker <docker>`. Let's create the file |
73 ``/tmp/hello.py`` that contains the following basic Python script:: | 73 ``/tmp/hello.py`` that contains the following basic Python script:: |
74 | 74 |
758 description. | 758 description. |
759 | 759 |
760 | 760 |
761 .. _python_authorization: | 761 .. _python_authorization: |
762 | 762 |
763 Forbid or allow access to REST resources (authorization) | 763 Forbid or allow access to REST resources (authorization, new in 3.0) |
764 ........................................................ | 764 .................................................................... |
765 | 765 |
766 .. highlight:: python | 766 .. highlight:: python |
767 | 767 |
768 The following Python script installs a callback that is triggered | 768 The following Python script installs a callback that is triggered |
769 whenever the HTTP server of Orthanc is accessed:: | 769 whenever the HTTP server of Orthanc is accessed:: |
867 print('Size of the frame: %dx%d' % (frame.GetImageWidth(), frame.GetImageHeight())) | 867 print('Size of the frame: %dx%d' % (frame.GetImageWidth(), frame.GetImageHeight())) |
868 | 868 |
869 orthanc.RegisterOnChangeCallback(OnChange) | 869 orthanc.RegisterOnChangeCallback(OnChange) |
870 | 870 |
871 | 871 |
872 | 872 .. _python_dicom_scp: |
873 | |
874 Handling DICOM SCP requests (new in 3.2) | |
875 ........................................ | |
876 | |
877 .. highlight:: python | |
878 | |
879 Starting with release 3.2 of the Python plugin, it is possible to | |
880 replace the C-FIND SCP and C-MOVE SCP of Orthanc by a Python | |
881 script. This feature can notably be used to create a custom DICOM | |
882 proxy. Here is a minimal example:: | |
883 | |
884 import json | |
885 import orthanc | |
886 import pprint | |
887 | |
888 def OnFind(answers, query, issuerAet, calledAet): | |
889 print('Received incoming C-FIND request from %s:' % issuerAet) | |
890 | |
891 answer = {} | |
892 for i in range(query.GetFindQuerySize()): | |
893 print(' %s (%04x,%04x) = [%s]' % (query.GetFindQueryTagName(i), | |
894 query.GetFindQueryTagGroup(i), | |
895 query.GetFindQueryTagElement(i), | |
896 query.GetFindQueryValue(i))) | |
897 answer[query.GetFindQueryTagName(i)] = ('HELLO%d-%s' % (i, query.GetFindQueryValue(i))) | |
898 | |
899 answers.FindAddAnswer(orthanc.CreateDicom( | |
900 json.dumps(answer), None, orthanc.CreateDicomFlags.NONE)) | |
901 | |
902 def OnMove(**request): | |
903 orthanc.LogWarning('C-MOVE request to be handled in Python: %s' % | |
904 json.dumps(request, indent = 4, sort_keys = True)) | |
905 | |
906 orthanc.RegisterFindCallback(OnFind) | |
907 orthanc.RegisterMoveCallback(OnMove) | |
908 | |
909 .. highlight:: text | |
910 | |
911 In this sample, the C-FIND SCP will send one single answer that | |
912 reproduces the values provided by the SCU:: | |
913 | |
914 $ findscu localhost 4242 -aet ORTHANC -S -k QueryRetrieveLevel=STUDY -k PatientName=TEST -k SeriesDescription= | |
915 I: --------------------------- | |
916 I: Find Response: 1 (Pending) | |
917 I: | |
918 I: # Dicom-Data-Set | |
919 I: # Used TransferSyntax: Little Endian Explicit | |
920 I: (0008,0005) CS [ISO_IR 100] # 10, 1 SpecificCharacterSet | |
921 I: (0008,0052) CS [HELLO0-STUDY] # 12, 1 QueryRetrieveLevel | |
922 I: (0008,103e) LO [HELLO1- ] # 8, 1 SeriesDescription | |
923 I: (0010,0010) PN [HELLO2-TEST ] # 12, 1 PatientName | |
924 I: | |
925 | |
926 The C-MOVE SCP can be invoked as follows:: | |
927 | |
928 $ movescu localhost 4242 -aem TARGET -aec LL -aet ORTHANC -S -k QueryRetrieveLevel=IMAGE -k StudyInstanceUID=1.2.3.4 | |
929 | |
930 The C-MOVE request above would print the following information in the | |
931 Orthanc logs:: | |
932 | |
933 W0610 18:30:36.840865 PluginsManager.cpp:168] C-MOVE request to be handled in Python: { | |
934 "AccessionNumber": "", | |
935 "Level": "INSTANCE", | |
936 "OriginatorAET": "ORTHANC", | |
937 "OriginatorID": 1, | |
938 "PatientID": "", | |
939 "SOPInstanceUID": "", | |
940 "SeriesInstanceUID": "", | |
941 "SourceAET": "LL", | |
942 "StudyInstanceUID": "1.2.3.4", | |
943 "TargetAET": "TARGET" | |
944 } | |
945 | |
946 It is now up to your Python callback to proces the C-MOVE SCU request, | |
947 for instance by calling the route ``/modalities/{...}/store`` in the | |
948 :ref:`REST API <rest-store-scu>` of Orthanc using | |
949 ``orthanc.RestApiPost()``. | |
950 | |
873 | 951 |
874 | 952 |
875 Performance and concurrency | 953 Performance and concurrency |
876 --------------------------- | 954 --------------------------- |
877 | 955 |