user3817250
user3817250

Reputation: 1043

Python TCP Socket Listener (Debian)

I'm getting started trying to write a Python TCP socket listener.

I have a Modem connected to my Debian box that reports GPS NMEA sentences every 60 seconds. For now all I want to do is have a python program running that will receive and print those GPS messages.

The modem is configured to send the messages to the Debian IP using TCP on port 8764.

I've created a simple program (based on http://pymotw.com/2/socket/tcp.html):

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 8764)
sock.connect(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print 'connection from', client_address

        while True:
            data = connection.recv(8)
            print 'received: %s' % data

    except:
        print 'error'

When I run this script it just sits at sock.accept(), after 'waiting for a connection'

I'm not sure if this is an issue in my Python code, or if I haven't properly configured the TCP socket in Debian.

Running netstat -tulpn I get:

tcp   0   0   127.0.0.1:8764   0.0.0.0:*   LISTEN   4327/python

Update:

Output from sudo tcpdump -i any port 8764

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
12:51:27.953851 IP 192.168.0.1.3202 > myHostName.local.8764: Flags [S], seq 1679917557, win 5200, options [mss 1300,sackOK,TS val 5634364 ecr 0,nop,wscale 1], length 0
12:51:27.953896 IP myHostName.local.8764 > 192.168.0.1.3202: Flags [R.], seq 0, ack 1679917558, win 0, length 0
12:51:57.970515 IP 192.168.0.1.3203 > myHostName.local.8764: Flags [S], seq 1717423935, win 5200, options [mss 1300,sackOK,TS val 5664379 ecr 0,nop,wscale 1], length 0
12:51:57.970561 IP myHostName.local.8764 > 192.168.0.1.3203: Flags [R.], seq 0, ack 1717423936, win 0, length 0
12:52:08.981407 IP 192.168.0.1.3204 > myHostName.local.8764: Flags [S], seq 1728493476, win 5200, options [mss 1300,sackOK,TS val 5675389 ecr 0,nop,wscale 1], length 0
12:52:08.981451 IP myHostName.local.8764 > 192.168.0.1.3204: Flags [R.], seq 0, ack 1728493477, win 0, length 0
12:52:39.064132 IP 192.168.0.1.3626 > myHostName.local.8764: Flags [S], seq 1770515829, win 5200, options [mss 1300,sackOK,TS val 5705470 ecr 0,nop,wscale 1], length 0
12:52:39.064175 IP myHostName.local.8764 > 192.168.0.1.3626: Flags [R.], seq 0, ack 1770515830, win 0, length 0

192.168.0.1 is the address of the GPS modem

So it appears that some sort of handshaking is happening every 30 seconds. The modem keeps jumping to new ports, perhaps because the previous communication attempt failed.


Update:

For some reason my server_address definition was the problem.

Exchanging server_address = ('localhost',8764) with server_address = ('192.168.0.4',8764) solved the issue

Upvotes: 1

Views: 2548

Answers (2)

umläute
umläute

Reputation: 31274

the problem is, that you are explicitely listening on the loopback device (lo, to which the IP 127.0.0.1 aka localhost is bound).

if you want to bind to all devices (and you don't care about the security implications), use the empty string as the host:

server_address = ('', 8764)

Upvotes: 1

ForceBru
ForceBru

Reputation: 44828

I think, it's normal behavior, as your code listens for connections. connection, client_address = sock.accept() gets executed only when you try to connect to the port on which your script's listening. You need to telnet localhost:9764 to get it to work.

I may have been mistaken, but you never mention that you try to connect to this port.

Upvotes: 2

Related Questions