Python Forum
Working socket() program...isn't
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working socket() program...isn't
#1
I'm pretty new to Python, and newer still to socket() but I've been working on learning it. I implemented the "Echo Client and Server" from https://realpython.com/python-sockets/#tcp-sockets and it was working fine Aug 11. It doesn't do much but it's a start. I've been away from this code since then, working on a RAW socket test but I haven't touched this one. I verified in my ssh console logs that everything in the py code is the same, and it's the same version of Python (3.6.6). There is a possibility that *something* changed - the CentOS 7 system on which this lives was added to our AD Domain a couple weeks after this was working. I put this in Networking in case it's particular to socket().

Code:
#!/usr/bin/python3.6
"""
sniffsms.py: listen on eth1 for traffic to the SMS modem device, and send a copy to the 
other host running the 'receive' end
"""

import socket, sys
import time
import logging

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

if len(sys.argv) < 2:

        print("server or client, idiot.")

        sys.exit(2)

if sys.argv[1] == 'server':

        logging.basicConfig(filename='/home/a-ptrivino/sserver.log', format='%(asctime)s %(message)s', level=logging.DEBUG)

        logging.info("Starting server.")

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as transfer:

                transfer.bind((HOST, PORT))

                transfer.listen()

                logging.info("Server listening on tcp/" + PORT + ".")

                conn, client = transfer.accept()

                with conn:

                        logging.info("Connected to " + str(client))

                        while True:

                                data = conn.recv(1024)

                                if not data:

                                        break

                                conn.sendall(data)

elif sys.argv[1] == 'client':

        logging.basicConfig(filename='/home/a-ptrivino/sclient.log', format='%(asctime)s %(message)s', level=logging.DEBUG)

        logging.info("Starting client.")

        connected = False

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sender:

                while not connected:

                        try:

                                sender.connect((HOST, PORT))

                                connected = True

                        except ConnectionRefusedError:

                               logging.info("Server not connected.")

                                time.sleep(3)

                sender.sendall(b'Test message.')

                data = sender.recv(1024)

        logging.info("Got back " + repr(data))

else:

        print("server or client, idiot.")
However, when I try to run it (always with sudo) I get this:

Output:
(pagesniff) [a-ptrivino@insecpydev pagesniff]$ sudo python sniffsms.py.bak server [sudo] password for a-ptrivino: Traceback (most recent call last): File "sniffsms.py.bak", line 26, in <module> with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as transfer: AttributeError: __exit__ (pagesniff) [a-ptrivino@insecpydev pagesniff]$
Can anyone say why this might have stopped working with no (specifically known) changes? BTW it fails with the same error on both 'server' and 'client' at the 'with' line.

Paul
Reply


Messages In This Thread
Working socket() program...isn't - by ptrivino - Nov-01-2019, 01:50 AM
RE: Working socket() program...isn't - by ptrivino - Nov-01-2019, 05:07 PM
RE: Working socket() program...isn't - by ptrivino - Nov-01-2019, 06:54 PM
RE: Working socket() program...isn't - by nilamo - Nov-01-2019, 07:14 PM
RE: Working socket() program...isn't - by ptrivino - Nov-01-2019, 07:51 PM
RE: Working socket() program...isn't - by nilamo - Nov-01-2019, 08:58 PM
RE: Working socket() program...isn't - by ptrivino - Nov-01-2019, 10:35 PM
RE: Working socket() program...isn't - by ptrivino - Nov-02-2019, 12:25 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020