简体   繁体   English

Python HL7 侦听器套接字消息确认

[英]Python HL7 Listener socket message acknowledgement

I am trying to create HL7 listener in python.我正在尝试在 python 中创建 HL7 侦听器。 I am able to receive the messages through socket , but not able to send valid acknowledgement我能够通过套接字接收消息,但无法发送有效确认

ack=u"\x0b MSH|^~\\&|HL7Listener|HL7Listener|SOMEAPP|SOMEAPP|20198151353||ACK^A08||T|2.3\x1c\x0d MSA|AA|153681279959711 \x1c\x0d"
ack = "MSH|^~\&|HL7Listener|HL7Listener|SOMEAPP|SOMEAPP|20198151353||ACK^A08||T|2.3 \r MSA|AA|678888295637322 \r"
ack= bytes(ack,'utf-8')

Python code :蟒蛇代码:

def listner_hl7():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.bind((socket.gethostname(), 4444))
    except Exception as e:
        print(str(e))
    s.listen(5)
    while True:
        clientSocket, addr = s.accept()
        message = clientSocket.recv(2048)
        id = (str(message.splitlines()[0]).split('|')[9])

        print('received {} bytes from {}'.format(
            len(message), addr))

        print('sending acknowledgement: ')
        ack = b"\x0b MSH|^~\\&|HL7Listener|HL7Listener|SOMEAPP|SOMEAPP|20198151353||ACK^A08||T|2.3\x1c\x0d MSA|AA|" + bytes(
            id, 'utf-8') + b" \x1c\x0d"

        clientSocket.send(ack)

I think your complete acknowledge is not being sent.我认为您没有发送完整的确认。 You are using clientSocket.send(ack) .您正在使用clientSocket.send(ack)

Use clientSocket.sendall(ack) instead.使用clientSocket.sendall(ack)代替。

Please refer to this answer from @kwarunek for more details.有关更多详细信息,请参阅@kwarunek 的答案。

socket.send is a low-level method and basically just the C/syscall method send(3) / send(2) . socket.send是一种低级方法,基本上只是 C/系统调用方法send(3) / send(2) It can send less bytes than you requested, but returns the number of bytes sent.它可以发送比您请求的更少的字节,但返回发送的字节数。

socket.sendall is a high-level Python-only method that sends the entire buffer you pass or throws an exception. socket.sendall是一种高级 Python 专用方法,它发送您传递的整个缓冲区或引发异常。 It does that by calling socket.send until everything has been sent or an error occurs.它通过调用socket.send直到所有内容都已发送或发生错误来socket.send这一点。

If you're using TCP with blocking sockets and don't want to be bothered by internals (this is the case for most simple network applications), use sendall .如果您使用带有阻塞套接字的 TCP 并且不想被内部结构打扰(大多数简单的网络应用程序都是这种情况),请使用sendall

尝试在 \\n 语法中在 MSA|AA 前面添加 enter 我认为它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM