每次更新数据时,从localhost将数据导入python

每次更新数据时,从localhost将数据导入python,python,Python,我在行中不断地对数据(整数和字符)进行gater,并在localhost:30003中输出它。我想要一个python脚本来收集每次在localhost:30003中输出的数据,并将其保存到我的case log.txt文件中。 我做了一个密码: import threading import urllib def printit(): threading.Timer(1, printit).start() #I set one second but in fact it doesn't ge

我在行中不断地对数据(整数和字符)进行gater,并在localhost:30003中输出它。我想要一个python脚本来收集每次在localhost:30003中输出的数据,并将其保存到我的case log.txt文件中。 我做了一个密码:

import threading
import urllib

def printit():
  threading.Timer(1, printit).start() #I set one second but in fact it doesn't get outputed at regular intervals
  feed = URLopener.retrieve('http://localhost:30003')
  f = open("log.txt", 'a')
  f.write(str(feed))
  f.close()

printit()
但它不做我想让它做的…,每秒钟都在打印:

<addinfourl at 1988080744 whose fp = <socket._fileobject object at 0x76b329b0>>

(或类似)

谢谢


Willfrd

您没有从URL读取数据。你不应该使用
f.write(str(feed))
,而应该使用
f.write(feed.read())

这是有效的

import threading
import urllib

def printit():
  threading.Timer(1, printit).start() #I set one second but in fact it doesn't get outputed at regular intervals
  feed = urllib.urlopen('http://localhost:30003')
  f = open("log.txt", 'a')
  f.write(str(feed.read()))
  f.close()

printit()

谢谢你的回答,但是仍然不起作用,现在它没有写任何东西:(它会抛出任何错误吗?你能打印
feed.read()吗?)
并检查输出否,它不输出任何内容…RaspberryPi中承载的本地主机连接到本地网络,因此我要做的是在我的计算机中对脚本进行更改,将脚本传递到RPi,并使用
python.py
从SSH运行它,我知道本地主机中已输出数据:30003,因为如果我键入I在控制台
wget localhost:30003
中,它将本地主机的内容保存在index.html文件中,一行输出的示例
MSG,4,1,134520f,12017/04/09,01:03:18.9292017/04/09,01:03:18.948,,,427207,,-64,,,,,,,0
但我希望python将其保存到以后过滤收集的数据。if
urllib.urlopen('http://localhost:30003“)。read()
不返回输出,服务器一定有问题。您能检查访问
http://localhost:30003
在您的web浏览器中,正在返回任何数据/尝试以这种方式使用,但不起作用,无法保存任何内容:(这表明源代码没有提供任何数据-您确定服务器工作正常吗?这是因为在RaspberryPi中,如果我在控制台中运行命令,则执行脚本
wget localhost:30003
我将数据输出到index.html文件中,数据如
MSG,4,1,134520f,12017/04/09,01:03:18.9201)2009年4月7日,01:03:18.948,,,427207,,,,-64,,,,,,,0
,但我想要的是python保存它,以便以后我可以过滤数据。