On Mon, 12 May 2008 08:34:07 -0700 (PDT), pe********@gmai l.com 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(s ocket.AF_INET, socket.SOCK_STR EAM) s.connect((HOS T, 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(s ocket.AF_INET, socket.SOCK_STR EAM) 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:\Documen ts and Settings\poupa\ Plocha\TCP3.py" , line 7, in <module>
s.connect((HOST , PORT))
File "<string>", line 1, in connect
File "C:\Python25\li b\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(204 8). 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 1 2252
On 12 Kvì, 17:54, Jean-Paul Calderone <exar...@divmod .comwrote:
On Mon, 12 May 2008 08:34:07 -0700 (PDT), petr.po...@gmai l.com 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(s ocket.AF_INET, socket.SOCK_STR EAM)
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(s ocket.AF_INET, socket.SOCK_STR EAM)
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:\Documen ts and Settings\poupa\ Plocha\TCP3.py" , line 7, in
<module>
s.connect((HOST , PORT))
File "<string>", line 1, in connect
File "C:\Python25\li b\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(204 8). 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: drs |
last post by:
Hi,
I have a program which opens a socket server in a thread. I need for the
server to listen on the socket for a certain amount of time (say, ten
seconds or so) and then close it, and am wondering if there is a good way to
close the socket other than sending it a message from another thread to
close itself. That is, I am looking for a way to forcably kill a socket
from a second thread, not to send it a message to kill itself.
...
|
by: Bernhard Schmidt |
last post by:
Hello,
sorry for bothering, I'm not a programmer and I don't do much python,
I'm more a networking guy trying to get his favourite linux distribution
to update through the shiny new protocol IPv6 again (for those who are
interested, I'm talking about Gentoo Linux)
Gentoo's portage system is implemented in python calling rsync to sync
with a mirror server. There are rotations (metahostnames with many
address records) where portage has...
|
by: Tom Opgenorth |
last post by:
I'm experiencing a problem with sockets, and I'm really hoping someone can
help me, please and thank you.
I've written a TCP Server, which listens on a port for an incoming
connection. When the client connects, the connection is NEVER to be closed
by the server. The client will send messages as necessary. After each
message the server is to send an acknowledgement back to the client. That is
the client's indicator to send the next...
|
by: Abubakar |
last post by:
Hi,
lets say I have a connected SOCKET s. At some point in time, I want to know
if the "s" is still valid, that it is still connected. Is there any API that
I can give me this information?
And can I register some callback like thing, that would inform me when "s"
disconnection happens? What I usually do is while I call "send" or "recv", I
get the socket_error and through that I know whats the status. But in this
situation actually I...
|
by: nephish |
last post by:
hey there,
i have a script that waits for message packets from a data server over
a socket.
it goes a little like this:
while 1:
x+=1
databack = sockobj.recv(158)
if databack:
| |
by: atlaste |
last post by:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnected" variable correctly...
|
by: O.B. |
last post by:
I have a socket configured as TCP and running as a listener. When I
close socket, it doesn't always free up the port immediately. Even
when no connections have been made to it. So when I open the socket
again, the bind fails because the port is still in use. When I
execute the code in "debug" mode, the problem never occurs. When I
execute the same code in release mode, the problem appears about 20%
of the time.
Here's the code:
|
by: petr.poupa |
last post by:
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))
|
by: Jean-Paul Calderone |
last post by:
On Mon, 12 May 2008 11:16:08 -0700 (PDT), petr.poupa@gmail.com wrote:
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.
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |