python将文件发送到作为服务运行的tika

2024-04-08

参考这个问题 https://stackoverflow.com/questions/16251436/unable-to-run-java-command-from-cgi我想将 MS Word (.doc) 文件发送到作为服务运行的 tika 应用程序,我该如何执行此操作?

有一个运行 tika 的链接:http://mimi.kaktusteam.de/blog-posts/2013/02/running-apache-tika-in-server-mode/ http://mimi.kaktusteam.de/blog-posts/2013/02/running-apache-tika-in-server-mode/

但是对于访问它的 python 代码,我不确定是否可以使用套接字或 urllib 或者到底是什么?


对于远程访问 Tika,基本上有两种方法可用。其一是蒂卡 JAXRS 服务器 https://wiki.apache.org/tika/TikaJAXRS,它提供了完整的 RESTful 接口。另一种是简单的Tika-App --服务器模式 https://issues.apache.org/jira/browse/TIKA-593,它仅适用于网络管道级别。

对于生产用途,您可能需要使用 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python将文件发送到作为服务运行的tika 的相关文章

随机推荐