我是编程和 Python 的新手。
我有一个非常基本的 python 脚本,可以连接到服务器并发送文本消息:
#!/usr/bin/python
import socket
s = socket.socket()
host = '127.0.0.1'
port = 4106
s.connect((host, port))
message = 'test1'
s.send(message)
print s.recv(1024)
s.close
一切都很好,除了这个消息是一个 HL7 消息,需要用 MLLP 包装 我找到了这个 API,我认为它可以为我做这件事 ( http://python-hl7.readthedocs.org/en/latest/api.html#mllp-network-client )
所以我将我的程序修改为以下内容,但我不断收到错误消息:NameError: name 'MLLPClient' is not defined
#!/usr/bin/python
import socket
import hl7
host = '127.0.0.1'
port = 4106
with MLLPClient(host, port) as client:
client.send_message('test1')
print s.recv(1024)
s.close
最佳答案
您可以通过不同的方式做到这一点;
如果导入顶层包
import hl7
您应该使用完整名称创建对象:
with hl7.client.MLLPClient(host, port) as client:
client.send_message('test1')
或者你可以只导入特定的类:
from hl7.client import MLLPClient
并像在示例中那样使用它。
参见 modules documentation获取更多信息。
关于Python 2.7 - 帮助使用 API (HL7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12665319/