python函数如何结束_如何在Python中结束函数
我正在修改从CSGO reddit收到的一些代码,使用刚刚发布的游戏状态集成来实现炸弹计时器,我设法使代码与HTTP Post JSON一起工作,如下所示:import time
import SimpleHTTPServer
import SocketServer
import logging
import cgi
import sys
import json
import signal
PORT = 3000
planted = False
#If we ctrl+C out of python, make sure we close the serial port first
#handler catches and closes it
def signal_handler(signal, frame):
ser.close()
sys.exit(0)
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
#SimpleHTTPServer doesn't handle post by default. Hacked up way to do it here
def do_POST(self):
length = int(self.headers["Content-Length"])
jsonString = str(self.rfile.read(length))
print jsonString
print "---"
jsonDict = json.loads(jsonString)
#From here we have a JSON dict, that has whatever data CS is sending
#For the timer, all we care about is the the 'round' key
if 'round' in jsonDict:
rounds = jsonDict['round']
if 'bomb' in rounds:
if rounds['bomb'] == "planted":
planted = True
print "Bomb has been planted"
#timer code here
def main():
x = 10
for i in range(x + 1):
time.sleep(1)
print(formatTime(x))
x -= 1
def formatTime(x):
minutes = int(x / 60)
seconds_rem = int(x % 60)
if (seconds_rem < 10):
return(str(minutes) + ":0" + str(seconds_rem))
else:
return(str(minutes) + ":" + str(seconds_rem))
main()
#If bomb has been defused, then send the 'C' message
if rounds['bomb'] == "defused":
planted = False
print "bomb has been defused"
#if the round ends, either bomb exploded or everyone died.
#Send the 'C' message to stop timer.
if 'previously' in jsonDict:
if 'round' in jsonDict['previously']:
if 'bomb' in jsonDict['previously']['round']:
planted = False
print "Round ran out"
#not sure if a response is really required. Send it anyway
response = bytes("This is the response.") #create response
self.send_response(200) #create header
self.send_header("Content-Length", str(len(response)))
self.end_headers()
self.wfile.write(response) #send response
Handler = ServerHandler
#On windows, Serial ports are usually COM1-3
#On mac/linux this will be different
#Set up our handler for ctrl+c
signal.signal(signal.SIGINT, signal_handler)
#Start server
httpd = SocketServer.TCPServer(("", PORT), Handler)
#Run server
httpd.serve_forever()
但是,这部分代码:
^{pr2}$
在pythonshell日志中显示了它自己运行了两次,它说炸弹已经被植入了,尽管这一轮已经完全结束了,但是否有人知道为什么它会自己运行两次。在
python函数如何结束_如何在Python中结束函数相关推荐
- python中用什么函数读取字符串_如何在Python中获得函数名作为字符串?
在Python中,如何在不调用函数的情况下以字符串的形式获得函数名? 1 2 3 4def my_function(): pass print get_function_name_as_string( ...
- python的matplotlib库内的函数_如何在matplotlib中找到函数下面的区域?
我是python和matplotlib库的新手,我试图在绘图中得到函数行下方的区域.我有一个变量a&:b,它在我的绘图中移动一个矩形.我也许可以使用原始数学来解决这个问题,但我想知道是否 ...
- python队列怎么用_如何在Python中使用多处理队列? - python
我很难理解多处理队列如何在python上工作以及如何实现它.假设我有两个python模块,它们从共享文件中访问数据,我们将这两个模块称为writer和Reader.我的计划是让读取器和写入器都将请求放 ...
- python字符串筛选输出_如何在Python中过滤字符串列表
Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...
- python隐藏启动台_如何在Python中启动后台进程?
如何在Python中启动后台进程? 我正在尝试将shell脚本移植到更易读的python版本. 原始shell脚本在后台使用"&"启动多个进程(实用程序,监视器等). 如何 ...
- python绘图背景透明_如何在 Matplotlib 中更改绘图背景
介绍Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库.在本教程中,我们将研究如何在Matplotlib中更改绘图的背景.导入数据 ...
- python获取当前时间戳_如何在Python中获取当前时间戳?
在Python中可以使用来自模块time.datetime或calendar的函数来获取当前时间戳,代码语句如[import time;ts = time.time() print(ts)]. 在Py ...
- python 运行r语言_如何在R中运行Python
python 运行r语言 尽管我很喜欢R,但很显然Python还是一种很棒的语言-既适用于数据科学又适用于通用计算. R用户想要在Python中做一些事情可能有充分的理由. 也许这是一个很棒的库,还没 ...
- python静态变量计数器_如何在Python中使用静态变量在计数
今天,在用Python写一个统计一个文件下有多少文件的小标本时,遇到了一个很棘手的问题.如何在Python中使用静态变量来计数.然后,就在网上一通查找,找的方法都是利用类的方法来实现静态变量.说实话没 ...
- python如何使用多态_如何在Python中为类应用多态?
介绍 多态性是为不同的底层形式(如数据类型或类)使用相同的界面的能力. 这允许功能在不同时间使用不同类型的实体. 对于Python中的面向对象编程,这意味着属于特定类的特定对象可以以与属于不同类的不同 ...
最新文章
- 初学者的深度学习训练与部署
- Unity 2D游戏开发教程之为游戏场景添加多个地面
- 将图片保存到系统相冊的两种方法
- 算法练习day6——190323(求中位数、堆排序、稳定性)
- SAP Cloud for Customer创建Account也会生成对应的Business partner实例
- c语言小数加分,求救:c语言课程设计 员工工资管理程序 有加分的。谢谢
- Visio各图形如何一键自动对齐?
- 实现Serclet(1)
- php和gps终端设备通讯,运输车辆GPS定位+语音对讲通讯方案
- paip.c#.net 多线程调用控件的问题
- c51单片机c语言交通灯的程序设计,基于51单片机的交通灯设计(三)-编写c51程序
- Linux下Weblogic部署安装
- Spring代码实例系列-绪论
- 记成功安装win10+elementary双系统
- Collectors
- 递归查找树形状结(利用steam流的方式)leval值标明
- Flink+kafka统计通话次数
- win7母机上的vmware12中Ubuntu16中安装复制粘贴工具
- crash report
- ios 项目被拒绝各种理由
热门文章
- 推荐时代的内容理解技术探索.pdf(附下载链接)
- SIGIR'21「微软」:强化学习过滤负样本噪声提升点击率
- dll文件用什么语言编写_为什么Unix不用功能更强大的C++而是用C编写
- linux性能测试cpu内存io,Linux下磁盘IO性能测试工具介绍
- 数据库mysql常用操作_mysql数据库常用操作
- 集成学习—Adaboost加性模型(论文研读)
- 一些值得注意的算法题——哈希表
- 西瓜书+实战+吴恩达机器学习(九)监督学习之k近邻 K-Nearest Neighbor
- MLflow机器学习工作流框架更新(2019.3)
- 人民邮电出版社与作者陈黎夫共同举办在线编辑、作者交流活动