Reputation: 57
I'm trying to create a TCP port scanner in Python that accepts a number of arguments (-all (displays all ports, both open and closed for the target), -open (only displays the open ports on the target), -target (specify target IP, subnet or hostname) and -range (specify port range).
Currently I've only managed to code the options used in the program, my code is as follows:
import optparse
parser = optparse.OptionParser()
parser.add_option('-all', dest='allPorts', help='displays all ports regardless of status')
parser.add_option('-open', dest='openPorts', help='displays only open ports')
parser.add_option('-target', type='string', dest='targetIP', help='specify target IP address, subnet or hostname')
parser.add_option('-range', type='string', dest='portRange', help='specify port range')
(options, args) = parser.parse_args()
I'm unsure how to continue with the program, particularly with the -all / -open options, any help would be greatly appreciated.
Upvotes: 2
Views: 903
Reputation: 152775
Normally you use str.join
to build a new string from several different strings:
''.join(data[:3])
and to replace o
with 0
use str.replace
:
''.join(data[:3]).replace('o', '0')
Note that you can get 3 samples with random.sample(data, 3)
, you don't need to shuffle the complete data
:
''.join(random.sample(data, 3)).replace('o', '0')
To exclude words containing "e"
you can only keep words that do not contain "e"
in the input:
with open('randomwords.txt', 'r') as f:
# a conditional list comprehension
data = [word for word in f.read().split() if "e" not in word]
[...]
Upvotes: 3
Reputation: 4684
I have modified one answer posted here, Actually i wanted to edit that answer but deleted by the author.
Try Following:
res = ""
for x in data[:3]:
res += x
res.replace("o", "0")
print res
OR
res = ""
for x in data[:3]:
res = res + x
print res.replace("o", "0")
Try Last one.
Upvotes: 1
Reputation: 36
First, your assignement seems to allow repetitions (e.g. passwords like "passw0rdpassw0rdpassw0rd") while your method doesn't. It is also inefficient. You can use random.choice
three times instead.
Concatenation of strings is done with +
operator, e.g. concatenation = str1 + str2 + str3
, or join
function. Replacing o with 0 is done with string class method replace
, e.g. concatenation.replace('o', '0')
.
Upvotes: 1