import os
import subprocess
def check_server():
cl = subprocess.Popen(["nmap","10.7.1.71"], stdout=subprocess.PIPE)
result = cl.communicate()
print result
check_server()
check_server()返回該元組:
('\nStarting Nmap 4.53 (http://insecure.org) at 2010-04-07 07:26 EDT\nInteresting ports on 10.7.1.71:\nNot shown: 1711 closed ports\nPORT STATE SERVICE\n21/tcp open ftp\n22/tcp open ssh\n80/tcp open http\n\nNmap done: 1 IP address (1 host up) scanned in 0.293 seconds\n', None)
改變的方法中的第二行至
result, err = cl.communicate()
導致check_server()返回:
Starting Nmap 4.53 (http://insecure.org) at 2010-04-07 07:27 EDT
Interesting ports on 10.7.1.71:
Not shown: 1711 closed ports
PORT STATE SERVICE
21/tcp open ftp
22/tcp open ssh
80/tcp open http
Nmap done: 1 IP address (1 host up) scanned in 0.319 seconds
看起來是元組轉換爲字符串的情況,\ n's被剝離......但是怎麼樣?
如果'__str __()'可用,'print'不使用'__repr __()',但標準序列類型使用'__repr __()'打印它們的元素。 – 2010-04-07 11:48:41