Socket and cycle problem

P

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
<module>
s.connect((HOST, PORT))
File "<string>", 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top