PageRenderTime 25ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/mllp.rst

https://github.com/johnpaulett/python-hl7
ReStructuredText | 121 lines | 94 code | 27 blank | 0 comment | 0 complexity | f0e9f646e57afe6b8fb86c90ef5a655c MD5 | raw file
  1. MLLP using asyncio
  2. ==================
  3. .. versionadded:: 0.4.1
  4. .. note::
  5. `hl7.mllp` package is currently experimental and subject to change.
  6. It aims to replace txHL7.
  7. python-hl7 includes classes for building HL7 clients and
  8. servers using asyncio. The underlying protocol for these
  9. clients and servers is MLLP.
  10. The `hl7.mllp` package is designed the same as
  11. the `asyncio.streams` package. `Examples in that documentation
  12. <https://docs.python.org/3/library/asyncio-stream.html>`_
  13. may be of assistance in writing production senders and
  14. receivers.
  15. HL7 Sender
  16. ----------
  17. .. code:: python
  18. # Using the third party `aiorun` instead of the `asyncio.run()` to avoid
  19. # boilerplate.
  20. import aiorun
  21. import hl7
  22. from hl7.mllp import open_hl7_connection
  23. async def main():
  24. message = 'MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4\r'
  25. message += 'PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|196203520|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520\r'
  26. message += 'OBR|1|845439^GHH OE|1045813^GHH LAB|1554-5^GLUCOSE|||200202150730||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^LEVEL SEVEN HEALTHCARE, INC.|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD\r'
  27. message += 'OBX|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F\r'
  28. # Open the connection to the HL7 receiver.
  29. # Using wait_for is optional, but recommended so
  30. # a dead receiver won't block you for long
  31. hl7_reader, hl7_writer = await asyncio.wait_for(
  32. open_hl7_connection("127.0.0.1", 2575),
  33. timeout=10,
  34. )
  35. hl7_message = hl7.parse(message)
  36. # Write the HL7 message, and then wait for the writer
  37. # to drain to actually send the message
  38. hl7_writer.writemessage(hl7_message)
  39. await hl7_writer.drain()
  40. print(f'Sent message\n {hl7_message}'.replace('\r', '\n'))
  41. # Now wait for the ACK message from the receiever
  42. hl7_ack = await asyncio.wait_for(
  43. hl7_reader.readmessage(),
  44. timeout=10
  45. )
  46. print(f'Received ACK\n {hl7_ack}'.replace('\r', '\n'))
  47. aiorun.run(main(), stop_on_unhandled_errors=True)
  48. HL7 Receiver
  49. ------------
  50. .. code:: python
  51. # Using the third party `aiorun` instead of the `asyncio.run()` to avoid
  52. # boilerplate.
  53. import aiorun
  54. import hl7
  55. from hl7.mllp import start_hl7_server
  56. async def process_hl7_messages(hl7_reader, hl7_writer):
  57. """This will be called every time a socket connects
  58. with us.
  59. """
  60. peername = hl7_writer.get_extra_info("peername")
  61. print(f"Connection established {peername}")
  62. try:
  63. # We're going to keep listening until the writer
  64. # is closed. Only writers have closed status.
  65. while not hl7_writer.is_closing():
  66. hl7_message = await hl7_reader.readmessage()
  67. print(f'Received message\n {hl7_message}'.replace('\r', '\n'))
  68. # Now let's send the ACK and wait for the
  69. # writer to drain
  70. hl7_writer.writemessage(hl7_message.create_ack())
  71. await hl7_writer.drain()
  72. except asyncio.IncompleteReadError:
  73. # Oops, something went wrong, if the writer is not
  74. # closed or closing, close it.
  75. if not hl7_writer.is_closing():
  76. hl7_writer.close()
  77. await hl7_writer.wait_closed()
  78. print(f"Connection closed {peername}")
  79. async def main():
  80. try:
  81. # Start the server in a with clause to make sure we
  82. # close it
  83. async with await start_hl7_server(
  84. process_hl7_messages, port=2575
  85. ) as hl7_server:
  86. # And now we server forever. Or until we are
  87. # cancelled...
  88. await hl7_server.serve_forever()
  89. except asyncio.CancelledError:
  90. # Cancelled errors are expected
  91. pass
  92. except Exception:
  93. print("Error occurred in main")
  94. aiorun.run(main(), stop_on_unhandled_errors=True)