Working socket() program...isn't - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Working socket() program...isn't (/thread-22148.html) Pages:
1
2
|
Working socket() program...isn't - ptrivino - Nov-01-2019 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: 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 RE: Working socket() program...isn't - Gribouillis - Nov-01-2019 Can you log the output of dir(socket.socket()) ?
RE: Working socket() program...isn't - ptrivino - Nov-01-2019 (Nov-01-2019, 06:32 AM)Gribouillis Wrote: Can you log the output of Quote:Python 3.6.6 (default, Aug 13 2018, 18:24:23) I know it has to do with context management, of which I understand the theory, and a lot of my searches for the error mention this. But what's really confounding is that this worked before and I haven't knowingly changed anything. Thanks for looking. RE: Working socket() program...isn't - Gribouillis - Nov-01-2019 Try to output the same thing from within your program, just before the with statement because obviously a strange object is created by your call to socket.socket() in the with statement. RE: Working socket() program...isn't - ptrivino - Nov-01-2019 (Nov-01-2019, 05:45 PM)Gribouillis Wrote: Try to output the same thing from within your program, just before the with statement because obviously a strange object is created by your call to socket.socket() in the with statement. Good call, obviously *something* has changed, __exit__ is no longer there: So I'm guessing somehow the socket() in the program is somehow being overridden since it's different from the one I tried in the interpreter.
RE: Working socket() program...isn't - nilamo - Nov-01-2019 Support for using sockets with the context manager (with-block) was added in 3.2. You said you've checked that they're both at least 3.6, but could you double check the python versions involved? I wouldn't think that'd be the issue, though, as the error would probably mention __enter__ instead of __exit__. Do you happen to have a file named "socket.py" in your path? Maybe you're importing the wrong thing... RE: Working socket() program...isn't - ptrivino - Nov-01-2019 (Nov-01-2019, 07:14 PM)nilamo Wrote: Support for using sockets with the context manager (with-block) was added in 3.2. You said you've checked that they're both at least 3.6, but could you double check the python versions involved? FYI (and maybe this is obvious from my 'outputs') I'm running in a virtual environment for Py3, but I see where you're going with this. So I added print(sys.version_info) print(socket.__file__)just above the first 'if', adn that yields: My path (from 'env') Thanks for this, BTW...I kinda see where this is headed. So I su'd and navigated to the directory, and ran it as root: It runs fine.
RE: Working socket() program...isn't - nilamo - Nov-01-2019 Quote:(pagesniff) [a-ptrivino@insecpydev pagesniff]$ sudo python sniffsms.py server sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0) /usr/lib64/python2.7/socket.pyc So, as not-root, python resolves to 2.7. What about sudo python3 sniffsms.py server to try to force python3?
RE: Working socket() program...isn't - Gribouillis - Nov-01-2019 I don't think sudo is the correct way to force python 3. Running as administrator serves radically different purposes than running another version of python. RE: Working socket() program...isn't - ptrivino - Nov-01-2019 (Nov-01-2019, 08:58 PM)nilamo Wrote:Quote:(pagesniff) [a-ptrivino@insecpydev pagesniff]$ sudo python sniffsms.py server sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0) /usr/lib64/python2.7/socket.pyc (Nov-01-2019, 09:46 PM)Gribouillis Wrote: I don't think sudo is the correct way to force python 3. Running as administrator serves radically different purposes than running another version of python. No, by running in the virtual environment (created with "python3.6 -m venv pagesniff" in this case) I get python v3.6.6 - you can see that from the first dir(socket.socket()) I posted (actually you can't, I cut that line off, but all I entered was 'python' and I get 3.6). Using sudo is necessary at this point to be able to use socket() - once this is fully put together it will be a systemd service and that need goes away. Hold up a bit on this - I just spoke with my Linux admin and he doesn't think it was the AD Domain add process, but possibly adding the system to our Ansible and/or Satelite setup that may have bit me. One thing I will try is creating a new virtual environment and see if THAT makes it happier. Paul |