Python HL7 Listener 套接字消息确认

标签 python sockets tcp hl7 mllp

我正在尝试在 python 中创建 HL7 监听器。我能够通过套接字接收消息,但无法发送有效确认

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 代码:

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)

最佳答案

我认为您的完整确认未发送。您正在使用 clientSocket.send(ack)

改用clientSocket.sendall(ack)

请引用this来自@kwarunek 的回答以获取更多详细信息。

socket.send is a low-level method and basically just the C/syscall method 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. It does that by calling socket.send until everything has been sent or an error occurs.

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.

关于Python HL7 Listener 套接字消息确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614298/

相关文章:

python - 在Python中用数字对字符串进行排序

javascript - 我们可以在 javascript 中打开 UDP 连接吗

同一台机器上的两个进程可以连接到同一个端口吗?

c - Winsock 只接受来自同一台计算机的连接,但不接受来自同一网络的连接

c++ - 使用 SFML TCP 套接字和数据包损坏文件

python 通过动态获取 url 的方式进行抓取

python - 舍入Python中的错误

python - Python 变量的循环列表

java - Netty channelRead()读写方法

c++ - 使用 send() 发送大于 MSS 的数据