ValueError:int()的基数为10的无效文字:' 20,'

时间:2016-02-03 22:23:30

标签: python

我目前正在阅读TJ的书#34; Violent Python。 O'Connor的"因为它是一种有趣的学习方式,但是当我尝试从书中复制代码时,我得到了错误。

[+] Scan Results for: 10.50.60.125
Traceback (most recent call last):
  File "portscanner.py", line 52, in <module>
    main()
  File "portscanner.py", line 50, in main
    portScan(tgtHost, tgtPorts)
  File "portscanner.py", line 35, in portScan
    t = Thread(target=connScan, arg=(tgtHost, int(tgtPort)))
ValueError: invalid literal for int() with base 10: '20,'

我运行代码的输入是

  

python portscanner.py -H 10.50.60.125 -p 20,1750

代码:

import optparse
import socket
from socket import *
from threading import *

screenLock = Semaphore(value=1)
def connScan(tgtHost, tgtPort):
    try:
        connSkt = socket(AF_INET, SOCK_STREAM)
        connSkt.connect((tgtHost, tgtPort))
        connSkt.send('ViolentPython\r\n')
        results = connSkt.recv(100)
        screenLock.acquire()
        print '[+]%d/tcp open'% tgtPort
        print '[+] ' + str(results)
    except:
        screenLock.acquire()
        print '[-]%d/tcp closed'% tgtPort
    finally:
        screenLock.release()
        connSkt.close()
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:
        t = Thread(target=connScan, arg=(tgtHost, int(tgtPort)))
        t.start()
def main():
    parser = optparse.OptionParser('usage%prog '+\
        '-H <target host> -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 parser.usage
        exit(0)
    portScan(tgtHost, tgtPorts)
if __name__ == "__main__":
        main()

我正在运行

  

Python 2.7.11 | Anaconda 2.4.1(64位)

我已经尝试扫描代码很长一段时间了,并且用Google搜索了很长一段时间,如果修复程序非常明显,我会道歉。

感谢。

1 个答案:

答案 0 :(得分:1)

Bash在命令的参数之间不需要逗号。将脚本调用更改为

python portscanner.py -H 10.50.60.125 -p 20 1750

注意,20之后没有逗号。