53716

Python asyncore UDP server

Question:

I am writing server application in Python that listens for requests, processes them, and sends a response.

All req/resp are send from the same address and port to the server application. I need to recv/send messages simultaneously, and the server need to recieve/send messages from/to the same port and address. I found some tutorials for asynchore sockets, but there are only examples for TCP connections.

Unfortunately, I need UDP. When I change SOCK_STREAM to SOCK_DGRAM in the create method, I get an error.

return getattr(self._sock,name)(*args) socket.error: [Errno 95] Operation not supported

I tried to use twisted, but I dont know how to write the sender part, which can bind to the same port as its listening. The last result was blocked port.

Is there any way how to use asyncore sockets with UDP or how to use twisted to send from the same port? Some examples will be higly appreciated.

Answer1:

You can pretty much just write the sending and receiving part of your code and they'll work together. Note that you can send and receive on a single listening UDP socket - you don't need one for each (particularly if you want to send from and receive on the same address).

from __future__ import print_function from sys import stdout from twisted.python.log import startLogging from twisted.internet import reactor from twisted.internet.protocol import DatagramProtocol class SomeUDP(DatagramProtocol): def datagramReceived(self, datagram, address): print(u"Got a datagram of {} bytes.".format(len(datagram))) def sendFoo(self, foo, ip, port): self.transport.write( (u"Foo datagram: {}".format(foo)).encode("utf-8"), (ip, port)) class SomeSender(object): def __init__(self, proto): self.proto = proto def start(self): reactor.callLater(3, self._send) def _send(self): self.proto.sendFoo(u"Hello or whatever", b"127.0.0.1", 12345) self.start() startLogging(stdout) proto = SomeUDP() reactor.listenUDP(12345, proto) SomeSender(proto).start() reactor.run()

Recommend

  • Receive multiple different messages TcpListener C#
  • Main Python Process is stopped using subprocess calls in SocketServer
  • Sending data on AF_PACKET socket
  • pyqt main window keeps crashing after receiving data from a thread
  • Human readable form of DNS lookup
  • Controlling prompt for command line
  • Creating and binding socket on Mac OS Hight Sierra
  • Connection reset by peer [errno 104] in Python 2.7
  • Python socket.error: [Errno 111] Connection refused on ubuntu 12.04
  • Change language on Firefox with Selenium Python
  • Winsock returns 10061 on connect only to localhost
  • Errno 13 Permission denied in django-wkhtmltopdf
  • How do I create a pie chart in Java
  • How does `super` interacts with a class's `__mro__` attribute in multiple inheritance?
  • Implementation of Thread-local storage (TLS) in C/C++ (multithreading)
  • GetQueuedCompletionStatus blocks forever
  • rvm and bundle install in Ubuntu 12.10: Errno::EACCES
  • nvm installed node and npm, symlink issue when installing local browserify
  • How to send an std::vector of unsigned char over an UDP socket using boost asio?
  • How to read data from socket connection - android
  • Cannot send user message with Spring Websocket
  • is this code truly private? (python)
  • Merge list of objects into consistent list based on common matching attribute in Python
  • Error in making a socket connection
  • What is the default HTTP verb in WebApi ? GET or POST?
  • TFS 2015 - Waiting for an agent to be requested
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Object and struct member access and address offset calculation
  • How to Cache Real-time Data?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?