Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sockets and Sendmail
#1
I have a socket client program that resends any received data to other connected clients.

I just added a function to send an email when certain data is received. I pass the data to the sendemail function and the email is sent.

However, when the program returns from the sendemail routine, I end up with a KeyError. If I comment out the actual sendmail() command, no KeyError is produced.

I can't figure out how these two things are related or what to do to fix it.

This all seems to work fine:

try:
  data = s.recv(64)
except socket.error, ex:
  print ex
if data:

# A readable client socket has data
    print '> ', data
    message_queues[s].put(data)

# Add output channel for response

if s not in outputs:
    outputs.append(s)
    update_html(data)
    checknewtrack(data)
    if (newtrack):
        sendemail()
But after coming back from the sendemail() function, it chokes on this code :

for s in writeable:
try:
    next_msg = message_queues[s].get_nowait() <---- KEYERROR HERE
except Queue.Empty:
    outputs.remove(s)
else:
    for t in connected_clients:
        t.send(next_msg)
Error:
Traceback (most recent call last): File "/home/user/bin/lora-gateway.py", line 192, in <module> do_work( True) File "/home/user/bin/lora-gateway.py", line 135, in do_work next_msg = message_queues[s].get_nowait() KeyError: <socket._socketobject object at 0x7f43a6617c20>
Any help would be appreciated.
Reply
#2
I get the feeling that since I'm using select.select() to handle socket input and output, firing something off with sendmail() might mess up that queue somehow. Not sure if that's possible or what to do about it if it is.
Reply
#3
It looks like the socket "s" you get from writables is not in message_queues dictionary.
Are you sure it is?

For best practice protect all this dictionary[key] calls with:

if key in dictionary:
  # stuff
else:
  # log/print error
EDIT: I'm also not sure it's wise to use a socket as a dictionary key, though I'm sure it's possible.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sockets interferring with USB ? epif18 0 2,635 Mar-19-2021, 07:45 PM
Last Post: epif18
  Just learning sockets etc... floatingshed 2 2,334 May-06-2020, 09:37 PM
Last Post: floatingshed
  Quick sockets question... ptrivino 2 2,222 Sep-26-2019, 08:51 PM
Last Post: ptrivino
  Script Conversion 2.7 to 3 (sockets) Pumpernickel 1 2,536 Apr-10-2019, 04:26 PM
Last Post: Pumpernickel
  What sort of things can I write to learn about sockets marienbad 2 2,696 Oct-16-2018, 04:02 PM
Last Post: buran
  unix domain sockets Skaperen 8 4,946 Sep-02-2018, 07:02 PM
Last Post: Skaperen
  file transfer with sockets sinister88 1 6,450 Nov-11-2017, 03:29 PM
Last Post: heiner55
  Trouble with sending raw sockets IronReign 2 4,199 Jun-01-2017, 08:26 AM
Last Post: camp0
  Silly Sockets Jarvis 2 4,209 Feb-20-2017, 01:43 PM
Last Post: Jarvis

Forum Jump:

User Panel Messages

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