python - 字符串到二进制python

标签 python networking byte

这对你们大多数人来说可能是一个愚蠢的问题,但我正面临这个问题;

我正在用 scapy 在网络上捕获一个 mac 地址,我通过这种方式将这个 mac 地址转换为 "\x00\x21\x23\x24\x25":

...
  b = p.sprintf("%Dot11.addr2%")
  print "Sending to this MAC Address",b
  #Here, the mac address looks like 00:00:00:00:00:00
  dstmac = str('\\x'+b.replace(':','\\x'))
  #Now, the mac address looks like this:
  "\x00\x00\x00\x00\x00\x00"
  sendp(dst=dstmac,blablablabla)
...

当此脚本通过网络发送 mac 时,它会发送以十六进制编码的实际\x(以及所有后续数字),而不是发送 00 00 00 00 00 00。

通常,你可以这样使用它,它很好:"sendp(dst="\x00\x00\x00\x00\x00\x00",blablablabla)

我认为我在进行此字符串操作或编码时做错了什么。

对此的任何输入都会有所帮助!

谢谢!

最佳答案

\x 符号用于字符串文字。而是将字符串解码为十六进制。

>>> '123456789abc'.decode('hex')
'\x124Vx\x9a\xbc'

关于python - 字符串到二进制python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11401750/

相关文章:

C# Silverlight 获取声音的频率

c++ - 使用字符数组存储数据

python - 执行脚本后使用 matplotlib 打开多个图形

python - python 中的错误栏 :ErrorbarContainer object of 3 artists

node.js - 如何使用字节范围提供播客(用于 iTunes 连接)

c# - 将数据从服务器顺序传输到多个客户端

c++ - WINAPI RegisterApplicationRestart 不清除打开的套接字

python - pytorch 耗尽 GPU 内存

Python 网络抓取请求遵循重定向

java - 如何获取扫描的wifi网络安全类型?在android中使用WiFi连接到特定网络所需的网络配置是什么?