Python3 TCP Server not seeing incoming messages from external device

ぐ巨炮叔叔 提交于 2019-12-24 18:52:47

问题


I want to create a small TCP server that takes incoming TCP connections from a device that is hooked up via Ethernet to my computer.

The physical port for that has the IP 192.168.1.100 statically assigned to it.

The scripts I use as a client and server are listed at the bottom.

The setup works if I want to send messages between the python scripts. However, I am unable to receive anything from the external device (screenshot from Wireshark capture below). From what I have read I can define an interface to listen to by defining its IP. So I defined the IP of the interface as the host variable. However, I do not receive anything in my script but the messages sent by the other script. I had a similar situation already here on stackoverflow. I thought that defining the correct IP as the host would resolve this issue but it did not.

I am also having a hard time capturing the traffic between the two scripts with Wireshark at all. They did not show up anywhere.

I need to pick up these connections on the eth0 interface with the static IP 192.168.1.100:

tcp_server.py

import socket

# create a socket object
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# get local machine name
# host = socket.gethostname()
host = "192.168.1.100"

port = 9002

# bind to the port
serverSocket.bind((host, port))

# queue up to 5 requests
serverSocket.listen(5)

while True:
    # establish a connection
    clientSocket, addr = serverSocket.accept()

    print("Got a connection from %s" % str(addr))

    msg = 'Thank you for connecting' + "\r\n"
    clientSocket.send(msg.encode('ascii'))
    clientSocket.close()

and this as a client:

tcp_client.py

import socket

# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# get local machine name
# host = socket.gethostname()
host = "192.168.1.100"

port = 9002

# connection to hostname on the port.
s.connect((host, port))

# Receive no more than 1024 bytes
msg = s.recv(1024)

s.close()
print(msg.decode('ascii'))

来源:https://stackoverflow.com/questions/50878441/python3-tcp-server-not-seeing-incoming-messages-from-external-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!