Here’s a quick snippet of python code that attempts to establish a TCP connection and reports whether it was successful or not. Netcat is a lot better suited for this role, but sometimes you may not be able to install it. In these situations this little script can come in handy:
- #!/usr/bin/env python
- import socket
- import time
- from time import sleep
- HOST=""
- PORT=""
- TIMEOUT=1.0
- WAIT = 1
- while True:
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.settimeout(TIMEOUT)
- s.connect((HOST, PORT))
- print "[%s] Connection established" % time.strftime("%H:%M:%S")
- time.sleep(1)
- s.close()
- except:
- print "[%s] Cannot connect" % time.strftime("%H:%M:%S")
- sleep(WAIT) # Sleep in sec before retry
* Python Stdlib - socket — Low-level networking interface
* [ Python 文章收集 ] 時間信息的獲取與表示簡介
* [Python Std Library] Data Types : datetime — Basic date and time types
Thanks for reminding! That's because the post is directly copy from my local machine and the corresponding js (javascript) isn't being uploaded to google blog. Sorry for inconvenience...^^"
回覆刪除