[Twisted-Python] send-msearch on all network interfaces of a machine
Hi, I am implementing a code through which i have to get devices connected to all network interfaces on my machine and list them . For this, i am first getting the ip of all network interfaces and then sending m-search command on them. After 2.5 seconds port is stopped to listen. But it is giving me some assertion error. (I am connected to 2 interfaces -ethernet and wifi) `Code:` class Base(DatagramProtocol): """ Class to send M-SEARCH message to devices in network and receive datagram packets from them """ SSDP_ADDR = "239.255.255.250" SSDP_PORT = 1900 MS = "M-SEARCH * HTTP/1.1\r\nHOST: {}:{}\r\nMAN: 'ssdp:discover'\r\nMX: 2\r\nST: ssdp:all\r\n\r\n".format(SSDP_ADDR, SSDP_PORT) def sendMsearch(self): """ Sending M-SEARCH message """ ports = [] for address in self.addresses: ports.append(reactor.listenUDP(0, self, interface=address)) for port in ports: for num in range(4): port.write(Base.MS, (Base.SSDP_ADDR,Base.SSDP_PORT)) reactor.callLater(2.5, self.stopMsearch, port) # MX + a wait margin def stopMsearch(self, port): """ Stop listening on port """ port.stopListening() `Error:` Traceback (most recent call last): File "work\find_devices.py", line 56, in sendMsearch ports.append(reactor.listenUDP(0, self, interface=address)) File "C:\Python27\lib\site-packages\twisted\internet\posixbase.py", line 374, in listenUDP p.startListening() File "C:\Python27\lib\site-packages\twisted\internet\udp.py", line 172, in startListening self._connectToProtocol() File "C:\Python27\lib\site-packages\twisted\internet\udp.py", line 210, in _connectToProtocol self.protocol.makeConnection(self) File "C:\Python27\lib\site-packages\twisted\internet\protocol.py", line 709, in makeConnection assert self.transport == None AssertionError This code worked: def sendMsearch(self): """ Sending M-SEARCH message """ address = socket.gethostbyname(socket.gethostbyname()) port = reactor.listenUDP(0, self, interface=address) for num in range(4): port.write(Base.MS, (Base.SSDP_ADDR,Base.SSDP_PORT)) reactor.callLater(2.5, self.stopMsearch, port) # MX + a wait margin Please tell what's wrong in code mentioned in the start of this mail and how to correct this. I have also posted this code on stackoverflow , but didn't get any response http://stackoverflow.com/questions/24178580/send-m-search-packets-on-all-net...
On 12:48 pm, pratik.prajapati12@gmail.com wrote:
[snip]
Please tell what's wrong in code mentioned in the start of this mail and how to correct this.
I have also posted this code on stackoverflow , but didn't get any response http://stackoverflow.com/questions/24178580/send-m-search-packets-on- all-network-interfaces-using-twisted-module-in-python
I posted an answer on stackoverflow. I previously hadn't seen the question because it didn't have the `twisted` tag (I edited the question as well to replace the `twisted.internet` tag with the `twisted` tag). I suggest you use `twisted` for stackoverflow questions about Twisted in the future. :) Jean-Paul
Thanks for the answer On Sat, Jun 14, 2014 at 7:57 PM, <exarkun@twistedmatrix.com> wrote:
On 12:48 pm, pratik.prajapati12@gmail.com wrote:
[snip]
Please tell what's wrong in code mentioned in the start of this mail and how to correct this.
I have also posted this code on stackoverflow , but didn't get any response http://stackoverflow.com/questions/24178580/send-m-search-packets-on- all-network-interfaces-using-twisted-module-in-python
I posted an answer on stackoverflow. I previously hadn't seen the question because it didn't have the `twisted` tag (I edited the question as well to replace the `twisted.internet` tag with the `twisted` tag). I suggest you use `twisted` for stackoverflow questions about Twisted in the future. :)
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- *Thanks and Regards,* *Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
Hi Jean-Paul, I have added a comment on your answer on stackoverflow, please respond there. On Sat, Jun 14, 2014 at 11:53 PM, Pratik Prajapati < pratik.prajapati12@gmail.com> wrote:
Thanks for the answer
On Sat, Jun 14, 2014 at 7:57 PM, <exarkun@twistedmatrix.com> wrote:
On 12:48 pm, pratik.prajapati12@gmail.com wrote:
[snip]
Please tell what's wrong in code mentioned in the start of this mail and how to correct this.
I have also posted this code on stackoverflow , but didn't get any response http://stackoverflow.com/questions/24178580/send-m-search-packets-on- all-network-interfaces-using-twisted-module-in-python
I posted an answer on stackoverflow. I previously hadn't seen the question because it didn't have the `twisted` tag (I edited the question as well to replace the `twisted.internet` tag with the `twisted` tag). I suggest you use `twisted` for stackoverflow questions about Twisted in the future. :)
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- *Thanks and Regards,*
*Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
-- *Thanks and Regards,* *Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
Hi Jean-Paul, you have seen my code on http://stackoverflow.com/questions/24178580/send-m-search-packets-on-all-net... code with your suggested changes this is working fine for windows machine, but on linux machine if no device is found on network then it doesn't go to stopMsearch() Please help. How to debug this ? On Sun, Jun 15, 2014 at 9:30 AM, Pratik Prajapati < pratik.prajapati12@gmail.com> wrote:
Hi Jean-Paul,
I have added a comment on your answer on stackoverflow, please respond there.
On Sat, Jun 14, 2014 at 11:53 PM, Pratik Prajapati < pratik.prajapati12@gmail.com> wrote:
Thanks for the answer
On Sat, Jun 14, 2014 at 7:57 PM, <exarkun@twistedmatrix.com> wrote:
On 12:48 pm, pratik.prajapati12@gmail.com wrote:
[snip]
Please tell what's wrong in code mentioned in the start of this mail and how to correct this.
I have also posted this code on stackoverflow , but didn't get any response http://stackoverflow.com/questions/24178580/send-m-search-packets-on- all-network-interfaces-using-twisted-module-in-python
I posted an answer on stackoverflow. I previously hadn't seen the question because it didn't have the `twisted` tag (I edited the question as well to replace the `twisted.internet` tag with the `twisted` tag). I suggest you use `twisted` for stackoverflow questions about Twisted in the future. :)
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- *Thanks and Regards,*
*Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
-- *Thanks and Regards,*
*Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
-- *Thanks and Regards,* *Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
Modified code (with changes suggested by you on stackoverflow): class Base(DatagramProtocol): """ Class to send M-SEARCH message to devices in network and receive datagram packets from them """ SSDP_ADDR = "239.255.255.250" SSDP_PORT = 1900 MS = "M-SEARCH * HTTP/1.1\r\nHOST: {}:{}\r\nMAN: 'ssdp:discover'\r\nMX: 2\r\nST: ssdp:all\r\n\r\n".format(SSDP_ADDR, SSDP_PORT) def sendMsearch(self): """ Sending M-SEARCH message """ timeInSeconds = [0.2,0.4,0.6,0.8] try: port = reactor.listenUDP(0, self, interface=self.address) for num, delay in zip(range(4), timeInSeconds): reactor.callLater(delay, port.write, Scanner.MS, (Scanner.SSDP_ADDR, Scanner.SSDP_PORT)) except error.CannotListenError: pass reactor.callLater(2.5, self.stopMsearch, port) # MX + a wait margin def stopMsearch(self, port): """ Stop listening on port """ port.stopListening() def findDevices(): addresses = [] for interface in interfaces(): try: for link in ifaddresses(interface)[AF_INET]: addresses.append(link['addr']) except KeyError: pass #loopback address if "127.0.0.1" in addresses: addresses.remove("127.0.0.1") for address in addresses: network = Base(address) network.sendMsearch() On Wed, Jun 25, 2014 at 3:50 PM, Pratik Prajapati < pratik.prajapati12@gmail.com> wrote:
Hi Jean-Paul,
you have seen my code on http://stackoverflow.com/questions/24178580/send-m-search-packets-on-all-net...
code with your suggested changes
this is working fine for windows machine, but on linux machine if no device is found on network then it doesn't go to stopMsearch()
Please help. How to debug this ?
On Sun, Jun 15, 2014 at 9:30 AM, Pratik Prajapati < pratik.prajapati12@gmail.com> wrote:
Hi Jean-Paul,
I have added a comment on your answer on stackoverflow, please respond there.
On Sat, Jun 14, 2014 at 11:53 PM, Pratik Prajapati < pratik.prajapati12@gmail.com> wrote:
Thanks for the answer
On Sat, Jun 14, 2014 at 7:57 PM, <exarkun@twistedmatrix.com> wrote:
On 12:48 pm, pratik.prajapati12@gmail.com wrote:
[snip]
Please tell what's wrong in code mentioned in the start of this mail and how to correct this.
I have also posted this code on stackoverflow , but didn't get any response http://stackoverflow.com/questions/24178580/send-m-search-packets-on- all-network-interfaces-using-twisted-module-in-python
I posted an answer on stackoverflow. I previously hadn't seen the question because it didn't have the `twisted` tag (I edited the question as well to replace the `twisted.internet` tag with the `twisted` tag). I suggest you use `twisted` for stackoverflow questions about Twisted in the future. :)
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- *Thanks and Regards,*
*Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
-- *Thanks and Regards,*
*Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
-- *Thanks and Regards,*
*Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
-- *Thanks and Regards,* *Pratik Prajapati* *Graduate Software Engineer* *Imagination Technologies India Pvt Ltd.*
participants (2)
-
exarkun@twistedmatrix.com
-
Pratik Prajapati