Why is this python UDP socket getting reset to 'None'? -
i have written python code sets class udpserver
containing udp socket. i'm initialising socket , binding localhost , particular port in constructor. reports success. have separate fellow member function hear out incoming info on socket. reports socket 'none'. question why? if move initialisation of socket out of constructor , listening function (i.e. init , listening in 1 function), socket not become 'none' , able receive info on it.
the code follows:
import socket class udpserver: def __init__(self, dst_ip, rcv_port, snd_port): self._dstip = dst_ip self._rcv_port = rcv_port self._snd_port = snd_port self._sock = self._create_socket() def _create_socket(self): # create datagram (udp) socket seek : self._sock = socket.socket(socket.af_inet, # net socket.sock_dgram) # udp print 'socket created.' except socket.error, msg : print 'failed create socket. error code : ' + str(msg[0]) + ' message ' + msg[1] sys.exit() # bind socket local host , port try: self._sock.bind(("", self._rcv_port)) except socket.error , msg: print 'bind failed. error code : ' + str(msg[0]) + ' message ' + msg[1] sys.exit() if(self._sock == none): print "self._sock none after bind!!" print 'socket bind complete.' def _receive_msg(self): print "waiting on port:", self._rcv_port while true: if(self._sock == none): print "self._sock none!" data, addr = self._sock.recvfrom(1024) # buffer size 1024 bytes print "received message:", info def main(): dst_ip = "100.1.11.275" udp_rcv_port = 30001 udp_snd_port = 30002 # create udp server udpserver = udpserver(dst_ip, udp_rcv_port, udp_snd_port) udpserver._receive_msg() if __name__ == "__main__": main()
the output below (i'm running windows dos prompt):
d:\python_tests>python udp_server.py socket created. socket bind complete. waiting on port: 30001 self._sock none! traceback (most recent phone call last): file "udp_server.py", line 51, in <module> main() file "udp_server.py", line 48, in main udpserver._receive_msg() file "udp_server.py", line 38, in _receive_msg data, addr = self._sock.recvfrom(1024) # buffer size 1024 bytes attributeerror: 'nonetype' object has no attribute 'recvfrom' d:\python_tests>
in contructor assigning self._sock
homecoming value of self._create_socket()
. problem is, self._create_socket()
not homecoming anything, or rather returns default value of none
.
if alter self._sock
in place, or homecoming function, works:
import socket class udpserver: def __init__(self, dst_ip, rcv_port, snd_port): self._dstip = dst_ip self._rcv_port = rcv_port self._snd_port = snd_port self._create_socket() def _create_socket(self): # create datagram (udp) socket seek : self._sock = socket.socket(socket.af_inet, # net socket.sock_dgram) # udp print 'socket created.' except socket.error, msg : print 'failed create socket. error code : ' + str(msg[0]) + ' message ' + msg[1] sys.exit() # bind socket local host , port try: self._sock.bind(("", self._rcv_port)) except socket.error , msg: print 'bind failed. error code : ' + str(msg[0]) + ' message ' + msg[1] sys.exit() if(self._sock == none): print "self._sock none after bind!!" print 'socket bind complete.' def _receive_msg(self): print "waiting on port:", self._rcv_port while true: if not self._sock: print "self._sock none!" data, addr = self._sock.recvfrom(1024) # buffer size 1024 bytes print "received message:", info def main(): dst_ip = "100.1.11.275" udp_rcv_port = 30001 udp_snd_port = 30002 # create udp server udpserver = udpserver(dst_ip, udp_rcv_port, udp_snd_port) udpserver._receive_msg() if __name__ == "__main__": main()
python sockets udp
No comments:
Post a Comment