Python 套接字模块 : How to change the local port on the client side?

标签 python sockets networking tcp port

我想从使用 TCP/IP 作为客户端的网络获取包。

与...

connect((TCP_IP, TCP_PORT))

...我可以更改对等地址的端口。但是我更改了本地计算机的端口?

编辑:我想使用有四个端口的网卡。网卡连接到发送大量数据的测量设备。如何查看数据的去向?如何区分这四个端口?

最佳答案

假设您有一个名为 sock 的套接字...使用 socket.bind()

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
DESTINATION_ADDR = '1.1.1.1'
SOURCE_PORT, DESTINATION_PORT = 31415, 80
sock.bind(('0.0.0.0', SOURCE_PORT))
sock.connect((DESTINATION_ADDR, DESTINATION_PORT))

现在我运行 tshark(文本 wireshark)来查看我在该接口(interface)上的流量...

[mpenning@Bucksnort ~]$ sudo tshark -i eth0 tcp and port 31415
Capturing on eth0
  0.000000 24.19.161.6 -> 1.1.1.1      TCP 31415 > http [SYN] Seq=0 Win=5840 Len=0 MSS=1460 TSV=4228124209 TSER=0 WS=6
 12.000009 24.19.161.6 -> 1.1.1.1      TCP 31415 > http [SYN] Seq=0 Win=5840 Len=0 MSS=1460 TSV=4228127209 TSER=0 WS=6
 36.000010 24.19.161.6 -> 1.1.1.1      TCP 31415 > http [SYN] Seq=0 Win=5840 Len=0 MSS=1460 TSV=4228133209 TSER=0 WS=6

关于Python 套接字模块 : How to change the local port on the client side?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6250198/

相关文章:

python - Python 2.7 的 Shebang 行

java - 点对点信使

android - 如何仅以编程方式为我的应用禁用移动数据

algorithm - 不清楚 Nagle 的算法

python - Django CMS 支持分类法或标签吗?

python - 如何使用 mysql-connector 检索日期时间值?

python - 如何使用 sqlite3 更新几个特定的​​列 - python

linux - Ubuntu Lucid 和 Trusty 之间的 Unix 域套接字权限

c++ - 在 C/C++ 中联网?

java - 如何通过 SOCKS 代理使用 URLConnection?