Socket and cycle problem

2008-05-12 Thread petr . poupa
Hello,
I am beginner but so I need help. I have small script for receive data
from port 3883, but it print only once.

import socket

HOST = 'localhost'
PORT = 3883
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(2048)
s.close()
print 'receive data from server:', `data`

So I try to write cycle to this script, like this:

import socket

HOST = 'localhost'
PORT = 3883
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while 1:
s.connect((HOST, PORT))
data = s.recv(2048)
s.close()
print 'receive data from server:', `data`

But Python reporting:

Traceback (most recent call last):
  File "C:\Documents and Settings\poupa\Plocha\TCP3.py", line 7, in

s.connect((HOST, PORT))
  File "", line 1, in connect
  File "C:\Python25\lib\socket.py", line 141, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: (9, 'Bad file descriptor')

Where is the mistake? I dont know.

thaks for help
Petr
--
http://mail.python.org/mailman/listinfo/python-list


Re: Socket and cycle problem

2008-05-12 Thread petr . poupa
On 12 Kvě, 17:54, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Mon, 12 May 2008 08:34:07 -0700 (PDT), [EMAIL PROTECTED] wrote:
> >Hello,
> >I am beginner but so I need help. I have small script for receive data
> >from port 3883, but it print only once.
>
> >import socket
>
> >HOST = 'localhost'
> >PORT = 3883
> >s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> >s.connect((HOST, PORT))
> >data = s.recv(2048)
> >s.close()
> >print 'receive data from server:', `data`
>
> >So I try to write cycle to this script, like this:
>
> >import socket
>
> >HOST = 'localhost'
> >PORT = 3883
> >s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> >while 1:
> >s.connect((HOST, PORT))
> >data = s.recv(2048)
> >s.close()
> >print 'receive data from server:', `data`
>
> >But Python reporting:
>
> >Traceback (most recent call last):
> >  File "C:\Documents and Settings\poupa\Plocha\TCP3.py", line 7, in
> >
> >s.connect((HOST, PORT))
> >  File "", line 1, in connect
> >  File "C:\Python25\lib\socket.py", line 141, in _dummy
> >raise error(EBADF, 'Bad file descriptor')
> >error: (9, 'Bad file descriptor')
>
> >Where is the mistake? I dont know.
>
> You cannot reconnect a socket.  You need to create a new one for each
> connection.  It's also almost certainly the case that the way you are
> receiving data is incorrect.  There is no guarantee that you will get
> 2048 bytes from socket.recv(2048).  It isn't even guaranteed that all
> the bytes written to the socket by the peer will be returned by such
> a call.  Instead, you need a framing protocol to determine when all
> data has been received.  For example, you might length prefix the
> data, or you might insert a delimiter (or a terminator) at the end.  Or
> if there is exactly one message to receive, then you should just read
> until the recv call returns '', indicating EOF.
>
> Jean-Paul

ok thanks, but I am true greenhorn, so you think that the best way is
write new script?
Could you send me code if it isnt long, becase I have no idea and
unfortunately time as well.

Petr
Petr
--
http://mail.python.org/mailman/listinfo/python-list


Re: Socket and cycle problem

2008-05-12 Thread petr . poupa
On 12 Kvě, 19:16, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Mon, 12 May 2008 09:19:48 -0700 (PDT), [EMAIL PROTECTED] wrote:
>
> > [snip]
>
> >> >Where is the mistake? I dont know.
>
> >> You cannot reconnect a socket.  You need to create a new one for each
> >> connection.  It's also almost certainly the case that the way you are
> >> receiving data is incorrect.  There is no guarantee that you will get
> >> 2048 bytes from socket.recv(2048).  It isn't even guaranteed that all
> >> the bytes written to the socket by the peer will be returned by such
> >> a call.  Instead, you need a framing protocol to determine when all
> >> data has been received.  For example, you might length prefix the
> >> data, or you might insert a delimiter (or a terminator) at the end.  Or
> >> if there is exactly one message to receive, then you should just read
> >> until the recv call returns '', indicating EOF.
>
> >> Jean-Paul
>
> >ok thanks, but I am true greenhorn, so you think that the best way is
> >write new script?
> >Could you send me code if it isnt long, becase I have no idea and
> >unfortunately time as well.
>
> I'm not sure what you want to happen, so I can't write it for you.  I
> suggest you start with Twisted instead of the socket module, though,
> as Twisted provides a higher-level interface to network programming.
>
> You can find some introductory documentation about writing clients here:
>
>  http://twistedmatrix.com/projects/core/documentation/howto/clients.html
>
> Jean-Paul


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'

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

thanks for reply
--
http://mail.python.org/mailman/listinfo/python-list


Re: Socket and cycle problem

2008-05-13 Thread petr . poupa
On 12 Kvě, 21:06, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Mon, 12 May 2008 11:16:08 -0700 (PDT), [EMAIL PROTECTED] 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

Thanks for code. I am trying both of them, but I am crash to another
problem.
For socket modul example python report:

Traceback (most recent call last):
  File "C:\Documents and Settings\poupa\Plocha\listen2.py", line 5, in

port.bind(('', 3883))
  File "", line 1, in bind
error: (10048, 'Address already in use')


and for twisted example pythod report:

Traceback (most recent call last):
  File "C:\Documents and Settings\poupa\Plocha\listen2twisted.py",
line 8, in -toplevel-
class PrintingProtocol(Protocol):
  File "C:\Documents and Settings\poupa\Plocha\listen2twisted.py",
line 28, in PrintingProtocol
print repr("".join(self.received))
NameError: name 'self' is not defined

How can I solve this errors?
(for twisted I instal zope for python, pywin32 and twisted. all for
python 2.4)




--
http://mail.python.org/mailman/listinfo/python-list


listening on TCP port

2008-05-07 Thread petr . poupa
Hello,
is there anybody who has experience with listening on TCP port with
python? I am looking for anything codes, tutorials,... .
Thanks for any information
--
http://mail.python.org/mailman/listinfo/python-list


listen on TCP port

2008-05-08 Thread petr . poupa
Hello,
is it possibble listening on TCP port by python and how? I am trying
chilkat but poorly :(.

Thanks
--
http://mail.python.org/mailman/listinfo/python-list