使用python读取流数据

时间:2015-10-07 17:09:27

标签: python stream

我有一个服务器ip:端口来自连续数据的来源。我想使用python读取这些数据,并希望在一些文件中传输。我在互联网上获得了这个示例代码,并根据我的需要进行了更改。

import socket
HOST = 'ip'   # use '' to expose to all networks
PORT = 10001

def incoming(host, port):
  """Open specified port and return file-like object"""
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  # set SOL_SOCKET.SO_REUSEADDR=1 to reuse the socket if
  # needed later without waiting for timeout (after it is
  # closed, for example)
  sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  sock.bind((host, port))
  sock.listen(0)   # do not queue connections
  request, addr = sock.accept()
  return request.makefile('r', 0)
# /-- network ---


for line in incoming(HOST, PORT):
  print line,

执行此操作后,我收到以下错误。

Traceback (most recent call last):
  File "read.py", line 24, in <module>
    for line in incoming(HOST, PORT):
  File "read.py", line 17, in incoming
    sock.bind((host, port))
  File "<string>", line 1, in bind
socket.error: [Errno 99] Cannot assign requested address

注意:当我使用网络浏览器访问ip:port时,我可以看到以文本格式打印的数据。

我该怎么办?

0 个答案:

没有答案