Re: Socket and cycle problem



On May 12, 2:06 pm, Jean-Paul Calderone <exar...@xxxxxxxxxx> wrote:
On Mon, 12 May 2008 11:16:08 -0700 (PDT), petr.po...@xxxxxxxxx wrote:
[snip]

My script send me via 3883 port (VRPN) data, but only once. I need
listening this port countinously.
So I need  make some loop to print data from 3883 port permanent.
Data that I recevied looks liek this:

receive data from server: 'vrpn: ver. 07.13  0\x00\xe8\x0b\x00\x00'

I'm not sure if you need to write a server or a client.  In your
original code, you had a client which repeatedly established out-
bound connections.  Here, you say you need to listen on a port.



Do you think its necessary to use Twisted? Do you have any ideas how
to do it with socket modul?

The basic version is pretty easy either way.  However, with Twisted,
you get cross-platform error handling without any extra effort, and
you don't have to think about the boring low-level details of BSD
sockets.

Here's a Twisted server that listens on port 3883 forever and prints
the data it receives from each connection after the remote side drops
the connection:

    from twisted.internet import reactor
    from twisted.internet.protocol import ServerFactory, Protocol

    class PrintingProtocol(Protocol):
        def connectionMade(self):
            """
            When the connection is first established, create a list
            into which to buffer all received data.
            """
            self.received = []

        def dataReceived(self, data):
            """
            Whenever any data is received on this connection, add it
            to the buffer.
            """
            self.received.append(data)

        def connectionLost(self, reason):
            """
            When the connection is lost, print out the contents of
            the receive buffer.
            """
            print repr("".join(self.received))

    # Create a factory which will use our protocol to handle incoming
    # connections.
    factory = ServerFactory()
    factory.protocol = PrintingProtocol

    # Listen with it on port 3883
    reactor.listenTCP(3883, factory)

    # Start the reactor.  Nothing in this program will ever stop the
    # reactor, so it will run and accept connections forever.
    reactor.run()

If you were to use the socket module, then it would look something like this:

    from socket import socket
    from errno import EINTR

    port = socket()
    port.bind(('', 3883))
    port.listen(5)
    while True:
        try:
            server, clientAddr = port.accept()
        except socket.error, e:
            print "Error accepting client connection", e
        else:
            received = []
            while True:
                try:
                    bytes = server.recv(1024 * 16)
                except socket.error, e:
                    if e.errno == EINTR:
                        continue
                    else:
                        break
                if not bytes:
                    break
                received.append(bytes)
            print repr("".join(received))

Hope this helps,

Jean-Paul

FWIW, my countrymen! The above code devotes a resource. It may be
trivial and/or microscopic, but a computer has them!
.



Relevant Pages

  • Re: Totally different IPs on the same LAN?
    ...     1) You have a router that has a public IP address on the WAN port ... address on the LAN port. ...     3) The router uses Network Address Translation to allow the home PC's ... the same network. ...
    (comp.dcom.lans.ethernet)
  • Re: network programming: how does s.accept() work?
    ... import socket ...     response.append ... The first client I started generated this output: ...
    (comp.lang.python)
  • Re: Tagged and Untagged ports
    ... I made 1 port on Cisco to be access port and another port to be trunk ...  switchport trunk allowed vlan 101 ... switch receives untagged is treated as being on VLAN 1, not VLAN 101, ...   - switch receives frame on port 17 and marks it internally for VLAN ...
    (comp.dcom.sys.cisco)
  • Re: Connecting inout signal to out.
    ... indicating that I'm trying to connect an inout port to an out port. ... -- VHDL automatically generated by Electric 8.06 for cell 'nand2' ...   component ground port; ...
    (comp.lang.vhdl)
  • Re: Bind on port fails while switch the user.
    ... application was not able to bind on port. ... If someone Know the reason for it and specific to MAC. ...    terminates and we try to restart it immediately. ...
    (comp.sys.mac.programmer.help)