开发者

Python socket-module: How to change the local port on the client side?

I want to get package开发者_运维问答s from a network using TCP/IP as a client.

With ...

connect((TCP_IP, TCP_PORT))

... I can change the port of the peer address. But I change the port of my local computer?

EDIT: I want to use a network card with four ports. The network card is connected to a measurement device which sends a lot of data. How can I see where the data goes? How can I distinguish between this four ports?


Assume you have a socket named sock... use 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))

Now I run tshark (text wireshark) to see my traffic on that 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


The four physical ports on your network adapter are not related to the logical port concept of IP (and TCP/IP).

Before we can help you further, please specify what is the configuration (i.e. IP address, subnet etc) of these physical ports. There might be a way to do what you want in Python: select one of the four ports for the traffic.


You can assign a TCP/IP client port, but you should not because it doesn't make any sense to do so. See TCP/IP Client (Ephemeral) Ports and Client/Server Application Port Use for an explanation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜