【发布时间】: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)
【问题讨论】:
-
抱歉,您的代码实际上看起来不错。完整的跟踪将很有帮助
-
print ("sent %s packets to %s through port %s" % (sent, ip, port)) -
这条线不在 python 3.6 上运行(测试)。我之前很困惑,因为我之前是从 python 2 运行的
标签: python-3.x