“错误:[Errno 10049]在我的python脚本中尝试发送udp数据包时,请求的地址在其上下文中无效”

时间:2016-11-19 18:19:23

标签: python-2.7

所以我得到了这个:

Traceback (most recent call last):
  File "E:\Demo Stand\test srcipts\attack_PLC.py", line 97, in <module>
    sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
error: [Errno 10049] The requested address is not valid in its context

这是我的代码:

import sys
import time
import socket


ans = True
ans1 = True
output01 = "" 
UDP_IP = str(output01)##if this is set to something like this "192.168.56.1" instead of output01 it works.
TankID = 0
UDP_PORT = int(TankID)
MESSAGE = "pump on"
MESSAGE2 = "fire"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

   while ans:

        output01 = raw_input("Enter target IP Address:")

        TankID = raw_input("Enter target ID:")
        TankID = TankID.strip(' \t\n\r')
        UDP_PORT = int(TankID)

        if output01 == output01 :
                print "Target Aquired"
                break
while ans1:
        output02 = raw_input("Environment Acquired....Press 'Y' to execute: ")
        if output02 == 'y':
                print "UDP target IP:", UDP_IP
                print "UDP target port:", UDP_PORT
                sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
                time.sleep(5)
                sock.sendto(MESSAGE2, (UDP_IP, UDP_PORT))
                break

我不知道为什么不只是发送数据包。当我将UDP_IP变量设置为任何IP地址时,就像“192.168.56.1”一样,它运行正常,但是当我从原始输入中存储它时,它会给我错误。

1 个答案:

答案 0 :(得分:0)

我认为这是因为您没有从raw_input转义回车符(\ n或\ r \ n): output01 = output01.strip('\ t \ n \ r') 可能会做到这一点。

你应该考虑检查Scapy的那种东西。