python - 如何在 Python 中获得完整的异常?

标签 python minecraft heartbeat python-multithreading

我正在用 Python 制作一个 Minecraft Classic 服务器。但是,我的代码中出现以下错误:

Unhandled exception in thread started by <bound method Heartbeat.start of <__main__.Heartbeat object at 0x01FC3810>>

这是我的心跳代码:

import httplib
import time
import thread

def heartbeat(salt = 'wo6kVAHjxoJcInKx', name = 'ElijahCraft', public = 'True', users = '0', maximum = '2', port = '25565'):
    listen = httplib.HTTPConnection('www.minecraft.net')
    listen.request('GET', '/heartbeat.jsp?port=' + port + '&max=' + maximum + '&name=' + name + '&public=' + public + '&version=7&salt=' + salt + '&users=' + users)
    url = listen.getresponse()
    return url.read()

class Heartbeat(object):
    def start(self, salt, name, public, users, maximum, port):
        print 'Server URL:', heartbeat(salt, name, public, users, maximum, port)
        self.users = users
        while not done:
            heartbeat(salt, name, public, self.users, maximum, port)
            time.sleep(45)

    def __init__(self, salt = 'wo6kVAHjxoJcInKx', name = 'ElijahCraft', public = 'True', users = '0', maximum = '2', port = '25565'):
        self.done = False
        thread.start_new_thread(self.start, (salt, name, public, users, maximum, port))

if __name__ == '__main__':
    heart = Heartbeat()

如何获得完整的回溯?

最佳答案

用 try/except 包装你的代码

import traceback
import sys

try:
    a = 1 /0


except Exception, inst:
    exc_traceback = sys.exc_info()[2]
    print str(inst) + "\n" + str(traceback.format_tb(exc_traceback)).replace('\\n', '\n')

关于python - 如何在 Python 中获得完整的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8929442/

相关文章:

python - python中的参数(-1,)是什么意思?

.net - 数据包拦截器 (.net)

python - 将从 url 获取的二进制数据保存到 Django FileField 中

python - "with"语句中的变量范围?

java - Java 如何获取网页的全部内容?

java - Bukkit myInventory 更改项目名称

class - 如何使用 Notepad++ 打开类文件?

linux - HA - 心跳和网络服务器

Linux HA/集群 : what are the differences between Pacemaker, 心跳、Corosync、wackamole?

python - 使用一个函数更新不同的类属性