簡體   English   中英

在python中使用udp客戶端從127.0.0.1接收響應的問題

[英]Issue with receiving response from 127.0.0.1 with udp client in python

我正在閱讀《 Black Hat Python》一書,並發現與端口80上的127.0.0.1的UDP連接有問題。我對這些東西還很陌生,所以我不能只是坐下來快速進行故障排除,高效,所以我希望有人可以指出一個錯誤,或者可能只是我的設置。 我正在使用VMware Fusion在Kali VM中對此進行測試。 套接字保持打開狀態並等待響應,但從未收到響應。

import socket

target_host = "127.0.0.1"
taget_port = 80

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto("AAABBBCCC", (target_host, taget_port))

data, addr = client.recvfrom(4096)

print data

具有客戶端和服務器的架構位於同一線程中是非常不尋常的。 我不推薦它。

但是,如果將套接字.bind()到目標地址,則程序將按照您想要的方式工作:

import socket

target_host = "127.0.0.1"
target_port = 8000

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.bind((target_host, target_port))
client.sendto("AAABBBCCC", (target_host, target_port))

data, addr = client.recvfrom(4096)

print data

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM