【发布时间】:2014-06-27 12:38:13
【问题描述】:
我正在编写我的 python 脚本以从我的服务器下载一个 xml 文件并将数据并行写入 sqlite3 数据库中。
关于如何使用 urllib2 库从我的服务器取消连接,我需要一些帮助。我希望数据库在连接被取消后立即停止写入数据。 allchannels_timer 将在用户点击“输入”按钮时被调用,该按钮将在连接到我的服务器下载 xml 文件之前等待一秒钟。当我点击退格按钮时,我想停止下载,但目前,代码继续运行,好像什么都没发生一样。
这是我当前的代码:
import urllib2
import StringIO
import sqlite3
import threading
from sqlite3 import dbapi2 as database
from xml.etree import ElementTree
import xml.etree.ElementTree as ET
from UserDict import DictMixin
#get actioncodes from keyboard.xml
ACTION_ENTER = 7
ACTION_BACKSPACE = 110
def cSetVisible(WiNdOw,iD,V=True): WiNdOw.getControl(iD).setVisible(V)
class MyClass(xbmcgui.WindowXML):
def timer1_8percent(self):
for i in range(1):
time.sleep(1)
self.getControl(4202).setLabel("8%")
def timer1_12percent(self):
for i in range(1):
time.sleep(2)
self.getControl(4202).setLabel("12%")
def timer1_18percent(self):
for i in range(1):
time.sleep(3)
self.getControl(4202).setLabel("18%")
def allchannels_timer(self):
for i in range(1):
time.sleep(0.3)
self.getControl(4202).setLabel("0%")
#DOWNLOAD THE XML SOURCE HERE
url = ADDON.getSetting('allchannel.url')
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
response.close()
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
self.getControl(4202).setLabel("1%")
self.thread = threading.Thread(target=self.timer1_8percent)
self.thread.setDaemon(True)
self.thread.start()
self.thread = threading.Thread(target=self.timer1_12percent)
self.thread.setDaemon(True)
self.thread.start()
self.thread = threading.Thread(target=self.timer1_18percent)
self.thread.setDaemon(True)
self.thread.start()
if os.path.exists(profilePath):
profilePath = profilePath + 'source.db'
con = database.connect(profilePath)
cur = con.cursor()
cur.execute('CREATE TABLE programs(channel TEXT, title TEXT, start_date TIMESTAMP, stop_date TIMESTAMP, description TEXT)')
con.commit()
con.close
tv_elem = ElementTree.parse(StringIO.StringIO(data)).getroot()
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
profilePath = profilePath + 'source.db'
con = sqlite3.connect(profilePath)
cur = con.cursor()
channels = OrderedDict()
# Get the loaded data
for channel in tv_elem.findall('channel'):
channel_name = channel.find('display-name').text
for program in channel.findall('programme'):
title = program.find('title').text
start_time = program.get("start")
stop_time = program.get("stop")
cur.execute("INSERT INTO programs(channel, title, start_date, stop_date)" + " VALUES(?, ?, ?, ?)", [channel_name, title, start_time, stop_time])
con.commit()
con.close
print 'Channels store into database are now successfully!'
program = None
now = datetime.datetime.now()
#strCh = '(\'' + '\',\''.join(channelMap.keys()) + '\')'
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel')
getprogram_info = cur.fetchall()
for row in getprogram_info:
programming = row[0], row[1], row[2], row[3]
print programming
#print row[0], row[1], row[2], row[3]
#programming = row[0], row[1], row[2], row[3]
#programming = row[0], row[1], row[2], row[3]
#cur.close()
def onAction(self, action):
img1_yellow = xbmc.getCondVisibility('Control.IsVisible(3)')
if action == ACTION_BACKSPACE:
if img1_yellow:
cSetVisible(self,3,True)
self.getControl(4202).setLabel("")
#cancel the connection and close the database
if action == ACTION_ENTER:
if img1_yellow:
cSetVisible(self,3,False)
self.thread = threading.Thread(target=self.allchannels_timer)
self.thread.setDaemon(True)
self.thread.start()
谁能告诉我如何取消与服务器的连接,以便停止下载 xml 文件。我还想知道在取消连接后如何防止数据写入我的数据库。
对于我的问题的任何帮助,我将不胜感激,我们将非常感谢一些示例代码。
【问题讨论】:
标签: python python-2.7 xbmc