这里有Python的初步经验,我希望能够打印出一些当前时间/日期作为第一项的文本.
到目前为止,这是我能够做到的,但看起来我在语法上是不正确的,有人可以帮我纠正吗?
import socket
import sys
import time
import datetime
remote_host = "127.0.0.1"
now = datetime.datetime.now()
for remote_port in [9002,8080]:
now_text = now.strftime("%Y-%m-%d %H:%M")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(60)
try:
sock.connect((remote_host, remote_port))
except Exception,e:
print "%d %d closed " % now_text remote_port
else:
print "%d %d open" % now_text remote_port
sock.close()
Run Code Online (Sandbox Code Playgroud)
亲切的问候
我想你正在寻找类似的东西
print "%d %d closed" % (now_text, remote_port)
Run Code Online (Sandbox Code Playgroud)
为了将来参考,这里有一种方法可以在Python 3中实现:
print("{0} {1} closed".format(now_text, remote_port))
Run Code Online (Sandbox Code Playgroud)
该.format()方法是在Python 2.6中引入的.
| 归档时间: |
|
| 查看次数: |
4017 次 |
| 最近记录: |