Python Forum
invalid keyword arguments error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: invalid keyword arguments error (/thread-27347.html)



invalid keyword arguments error - sagpal - Jun-03-2020

Hi,
can anyone help me with this.


import optparse
import socket
from socket import *


def connScan(tgtHost, tgtPort):
    try:
        connSkt = socket(AF_INET, SOCK_STREAM)
        connSkt.connect((tgtHost, tgtPort))
        connSkt.send('ViolentPython\r\n')
        results = connSkt.recv(100)
        print('[+]%d/tcp open' % tgtPort)
        print('[+]' + str(results))
        connSkt.close()
    except:
        print('[-]$d/tcp closed' % tgtPort)


def portScan(tgtHost, tgtPorts):
    try:
        tgtIP = gethostbyname(tgtHost)
    except:
        print("[-] Cannot resolve '%s': Unknown host" % tgtHost)
        return
    try:
        tgtName = gethostbyaddr(tgtIP)
        print('\n[+] Scan Results for: ' + tgtName[0])
    except:
        print('\n[+] Scan results for: ' + tgtIP)
    setdefaulttimeout(1)
    for tgtPort in tgtPorts:
        print('Scanning port' + tgtPort)
        connScan(tgtHost, int(tgtPort))


def main():
    parser = optparse.OptionParser(
        "usage%prog " + "-H <target hsot> -p <target port>")
    parser.add_option('-H', dest='tgtHost', type='string',
                      help='specify target host')
    parser.add_option('-p', desk='tgtPort', type='string',
                      help='specify target port[s] separated by comma')
    (options, args) = parser.parse_args()
    tgtHost = options.tgtHost
    tgtPorts = str(options.tgtPort).split(', ')
    if (tgtHost == None) | (tgtPorts[0] == None):
        print('[-] You must specify a target host and part[s].')
        exit(0)
    portScan(tgtHost, tgtPorts)


if __name__ == '__main__':
    main()
Error:
Exception has occurred: OptionError option -p: invalid keyword arguments: desk File "D:\Skill\python\py\port_scan.py", line 41, in main parser.add_option('-p', desk='tgtPort', type='string', File "D:\Skill\python\py\port_scan.py", line 53, in <module> main()



RE: port_scan - Yoriz - Jun-03-2020

You have a typo in
parser.add_option('-p', desk='tgtPort', type='string',
    help='specify target port[s] separated by comma')
desk instead of dest


RE: invalid keyword arguments error - sagpal - Jun-04-2020

thanks.
Now I am getting typeError in the output.

import optparse
import socket
from socket import *


def connScan(tgtHost, tgtPort):
    try:
        connSkt = socket(AF_INET, SOCK_STREAM)
        connSkt.connect((tgtHost, tgtPort))
        connSkt.send('ViolentPython\r\n')
        results = connSkt.recv(100)
        print('[+]%d/tcp open' % tgtPort)
        print('[+]' + str(results))
        connSkt.close()
    except:
        print('[-]$d/tcp closed' % tgtPort)


def portScan(tgtHost, tgtPorts):
    try:
        tgtIP = gethostbyname(tgtHost)
    except:
        print("[-] Cannot resolve '%s': Unknown host" % tgtHost)
        return
    try:
        tgtName = gethostbyaddr(tgtIP)
        print('\n[+] Scan Results for: ' + tgtName[0])
    except:
        print('\n[+] Scan results for: ' + tgtIP)
    setdefaulttimeout(1)
    for tgtPort in tgtPorts:
        print('Scanning port' + tgtPort)
        connScan(tgtHost, int(tgtPort))


def main():
    parser = optparse.OptionParser("usage%prog " +
                                   "-H <target hsot> -p <target port>")
    parser.add_option('-H', dest='tgtHost', type='string',
                      help='specify target host')
    parser.add_option('-p', dest='tgtPort', type='string',
                      help='specify target port[s] separated by comma')
    (options, args) = parser.parse_args()
    tgtHost = options.tgtHost
    tgtPorts = str(options.tgtPort).split(', ')
    if (tgtHost == None) | (tgtPorts[0] == None):
        print('[-] You must specify a target host and part[s].')
        exit(0)
    portScan(tgtHost, tgtPorts)


if __name__ == '__main__':
    main()
Output:
[+] Scan Results for: TEST-LAB Scanning port21 Traceback (most recent call last): File "port_scan.py", line 9, in connScan connSkt.connect((tgtHost, tgtPort)) socket.timeout: timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "port_scan.py", line 53, in <module> main() File "port_scan.py", line 49, in main portScan(tgtHost, tgtPorts) File "port_scan.py", line 33, in portScan connScan(tgtHost, int(tgtPort)) File "port_scan.py", line 16, in connScan print('[-]$d/tcp closed' % tgtPort) TypeError: not all arguments converted during string formatting



RE: invalid keyword arguments error - bowlofred - Jun-04-2020

I don't know this module well, but 'tgtPort' is specified as a "string", while the module info suggests that for AF_INET, the port should be an integer.

You may want to parse it as an integer, or transpose it to one before passing it to connect().