TypeError:是否需要一个整数(类型为str)?

时间:2018-10-17 02:40:22

标签: python-3.x

我在发送数据包时遇到麻烦,我将较旧的代码从python的较旧版本导出到了python3。我收到TypeError:需要输入整数(错误类型为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)

2 个答案:

答案 0 :(得分:1)

limit = n$query = "SELECT `firstName`, `lastName`, `nickName`, `cellNumber` , `workNumber` , `homeNumber` , `birthday` , `memo` FROM contacts LIKE '%".$valueToSearch."%' WHERE userId = '{$_SESSION['user_id']}' "; ip必须是我认为的整数值。 因此,这应该可以解决-

port

答案 1 :(得分:0)

根据documentationport方法的地址元组中的socket.sendto()项必须是整数,因此应将input()的返回值转换为port转换为整数:

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