【问题标题】:TypeError: an integer is required (got type str)?TypeError:需要一个整数(得到类型 str)?
【发布时间】:2018-10-18 05:03:04
【问题描述】:

我的数据包发送代码时遇到问题,我将旧代码从旧版本的 python 导出到 python 3。我收到 TypeError: an integer is required (got type str) 错误,我不知道如何修复它.

import socket
import random
import time

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

bytes = random._urandom(1024)

ip = int(input('Target ip:'))
port = int(input('port:'))
duration = int(input('number of seconds to send packets: '))
timeout = time.time() + float(duration)
sent = 0
while True:
    if time.time() > timeout:
        break
    else:
        pass
    sock.sendto(bytes,(ip,port))
    sent = sent + 1
    print ("sent %s packets to %s through port %s")%(sent, ip, port)

我是这个论坛的新手,在此先抱歉。

这是我的完整回溯

    Target ip:157.240.22.35:
port:8080
number of seconds to send packets: 2
Traceback (most recent call last):
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py", line 119, in <module>
    vspd.debug(filename, port_num, debug_id, debug_options, run_as)
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\debugger.py", line 37, in debug
    run(address, filename, *args, **kwargs)
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_local.py", line 48, in run_file
    run(argv, addr, **kwargs)
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_local.py", line 99, in _run
    _pydevd.main()
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\pydevd.py", line 1734, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\pydevd.py", line 1079, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\pydevd.py", line 1086, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\Users\nikit\source\repos\PythonApplication2\PythonApplication2\PythonApplication2.py", line 19, in <module>
    sock.sendto(bytes,(ip,port))
TypeError: an integer is required (got type str)

【问题讨论】:

标签: python-3.x


【解决方案1】:

ipportduration 我想必须是 int 值。 所以,这应该可以解决它-

ip = input('Target ip:')
port = int(input('port:'))
duration = int(input('number of seconds to send packets: '))

【讨论】:

  • 我知道在输入示例 IP 地址时会出现新错误。在使用您的整数解决方案时,当我在运行它时输入我的 IP 时,我得到了这个。回溯(最后一次调用):文件“python”,第 9 行,在 中 ValueError:int() 的无效文字,基数为 10:'157.240.22.35:'
  • 对不起,我之前把IP转成int,应该是str,所以不应该投。更新了我的答案。
【解决方案2】:

socket.sendto()方法的地址元组中的port项根据documentation必须是整数,所以你应该将portinput()的返回值转换为整数:

port = int(input('port:'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-14
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多