所以我正在编写一个简单的套接字程序。客户端将目录路径发送到服务器。 '数据'是给定的目录路径。在下面的示例中,它采用给定的路径并运行Linux命令' ls -l path'。
while True:
print (data.decode())
batcmd=('ls -l ' + data.decode())
result = os.system(batcmd)
因此,如果客户输入' / home / user',则shell命令' ls -l / home / user'将运行并显示目录的内容和权限。
现在我想对结果进行排序并对特定单词进行细化。然后显示包含该单词的行。 例如,一行包含单词" Pictures"。 所以我想显示那条
的那一行drwxr-xr-x 3 myname myname 4096 Feb 25 2017 Pictures
如果我试试这个:
import subprocess
import os
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 2000
SIZE = 1024
hostName = gethostbyname( '0.0.0.0' )
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
# Receive no more than 1024 bytes
while True:
(data,addr) = mySocket.recvfrom(SIZE)
try:
print >>sys.stderr, 'Received data from', addr
while True:
print (data.decode())
batcmd=('ls -l ' + data.decode())
result = os.system(batcmd)
word = "Pictures"
print result.find(word)
# Do other stuff here
finally:
print >> sys.stderr, 'closing socket'
sys.exit()
我收到错误" AttributeError:' int'对象没有属性'找到'"
如果我试试这个:
import subprocess
import os
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 2000
SIZE = 1024
hostName = gethostbyname( '0.0.0.0' )
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
# Receive no more than 1024 bytes
while True:
(data,addr) = mySocket.recvfrom(SIZE)
try:
print >>sys.stderr, 'Received data from', addr
while True:
print (data.decode())
batcmd=('ls -l ' + data.decode())
result = os.system(batcmd)
word = "pictures"
for line in result:
search_res = word(line)
print line
if search_res is None:
print ("That word does not exist.")
break
else:
print ("something else")
# Do other stuff
finally:
print >> sys.stderr, 'closing socket'
sys.exit()
我得到TypeError:' int'对象不可迭代
查找单词并打印包含该单词的行的最佳方法是什么?
我是否需要将结果转储到文件中才能处理它?</ p>