每次更新时,将数据从本地主机导入 python



我不断地将数据(中间人和字符(放在行中,并在localhost:30003中输出。我希望python脚本每次在localhost:30003中输出数据时收集数据,并将其保存到我的案例日志.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>>

(或类似(

谢谢

威尔弗德

您没有从 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()

最新更新