Reference to this question我想将 MS Word (.doc) 文件发送到作为服务运行的 tika 应用程序,我该如何执行此操作?
有一个运行 tika 的链接: http://mimi.kaktusteam.de/blog-posts/2013/02/running-apache-tika-in-server-mode/
但是对于要访问它的 python 代码,我不确定是否可以使用套接字或 urllib 或者到底是什么?
最佳答案
对于远程访问 Tika,基本上有两种方法可用。一是Tika JAXRS Server ,它提供了完整的 RESTful 接口(interface)。另一个是简单的Tika-App --server mode ,它仅适用于网络管道级别。
对于生产用途,您可能需要使用 Tika JAXRS 服务器,因为它功能更齐全。对于简单的测试和入门,服务器模式下的 Tika 应用程序应该没问题
对于后者,只需连接到运行 Tika-App 的端口,将文档数据传输到其中,然后读回 html。例如,在一个终端中运行
$ java -jar tika-app-1.3.jar --server --port 1234
然后,在另一个中,执行
$ nc 127.0.0.1 1234 < test.pdf
然后您将看到测试 PDF 返回的 html
从 python 中,您只需要一个简单的套接字调用,就像 netcat 所做的那样,发送二进制数据,然后读回结果。例如,尝试以下操作:
#!/usr/bin/python
import socket, sys
# Where to connect
host = '127.0.0.1'
port = 1234
if len(sys.argv) < 2:
print "Must give filename"
sys.exit(1)
filename = sys.argv[1]
print "Sending %s to Tika on port %d" % (filename, port)
# Connect to Tika
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
# Open the file to send
f = open(filename, 'rb')
# Stream the file to Tika
while True:
chunk = f.read(65536)
if not chunk:
# EOF
break
s.sendall(chunk)
# Tell Tika we have sent everything
s.shutdown(socket.SHUT_WR)
# Get the response
while True:
chunk = s.recv(65536)
if not chunk:
# EOF
break
print chunk
关于python将文件发送到作为服务运行的tika,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361254/