Hello,
I have a short program that the client make a connection to server, a thread is then created to poll any data sent from the server. The main thread will close the socket after 5 seconds. Here are the code:
from socket import *
import select
import threading
import time
def poll(c):
i, o, e = select.select([c], [], [])
if not i:
print "time out"
return
print i
data = i[0].recv(1024)
print "data: ", data
if __name__=="__main__":
c = socket(AF_INET, SOCK_STREAM)
c.connect(('192.168.100.74', 8888))
th=threading.Thread(None, poll, "", (c, ))
th.setDaemon(1)
th.start()
time.sleep(5)
c.shutdown(2)
c.close()
th.join()
print "completed"
On Windows, as soon as client socket 'c' is closed, the select() call returns. However, on Linux, the program seems blocking forever ( may be I am not patient to wait ). Is there anything wrong with my code?
Regards,
-- Wong 2 1464
On Wed, 2 Jun 2004 10:37:21 +0800, "Joe Wong" <jo*****@mango.cc>
wrote: Hello,
I have a short program that the client make a connection to server, a thread is then created to poll any data sent from the server. The main thread will close the socket after 5 seconds. Here are the code:
from socket import * import select import threading import time
def poll(c): i, o, e = select.select([c], [], []) if not i: print "time out" return print i data = i[0].recv(1024) print "data: ", data
if __name__=="__main__": c = socket(AF_INET, SOCK_STREAM) c.connect(('192.168.100.74', 8888)) th=threading.Thread(None, poll, "", (c, )) th.setDaemon(1) th.start()
time.sleep(5) c.shutdown(2) c.close() th.join() print "completed"
On Windows, as soon as client socket 'c' is closed, the select() call returns. However, on Linux, the program seems blocking forever ( may be I am not patient to wait ). Is there anything wrong with my code?
Regards,
-- Wong
No, your code is ok. It's just that closing the local end of the
socket is undefined in select(). Which is why Windows does one thing
and Linux the other.
Closing the remote end causes the socket to show up as readable with
zero data.
<{{{*>
fishboy <fi*****@spamspamspam.com> wrote in message news:<82********************************@4ax.com>. .. On Wed, 2 Jun 2004 10:37:21 +0800, "Joe Wong" <jo*****@mango.cc> wrote:
snip No, your code is ok. It's just that closing the local end of the socket is undefined in select(). Which is why Windows does one thing and Linux the other.
Closing the remote end causes the socket to show up as readable with zero data.
Precisely. I've been through this myself. My solution on the FreeBSD
server side is (pardon the bad pseudocode):
rd, wr, err = select(myClientSocksList, [], myClientSocksList,
myTimeOut)
for eachSock in rd:
try:
dataIn = eachSock.recv(MaxBufSz)
except socket.error:
# Server closes session.
self.closeSession(eachSock, "Socket error - recv")
else:
if len(dataIn) > 0:
# Process, process.
else:
# Server closes session.
self.closeSession(eachSock, "Recvd 0 bytes")
# Check the err list etc, etc. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Markus Franz |
last post by:
Hi.
Today I created a script called load.py for using at the command line
written in Python 2.3.
This script should load as many websites as given on the comand line and
print them with a...
|
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...
|
by: huy |
last post by:
Hi,
I'm using cherrypy to provide a user interface for users to start a
linux server program eg. os.system("nohup myserver.py &"). The problem
is that if I stop cherrypy server and restart, I...
|
by: Grzegorz Kaczor |
last post by:
Hello all,
I've got a VERY strange network problem with Win2k Server and .NET.
I've got one central server (hub) getting raw binary data (files) from
many locations. Both server and clients...
|
by: Grzegorz Kaczor |
last post by:
Hello,
I've got a VERY strange network problem with Win2k Server and .NET.
I've got one central server (hub) getting raw binary data (files)
from many locations. Both server and clients are...
|
by: Rob White |
last post by:
I have two fairly simple applications, one is a management app that
multicasts requests for status to a number of clients, most of the time
the clients happily receive the multicast and respond in...
|
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...
|
by: FBM |
last post by:
Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
...
|
by: Mirko Vogt |
last post by:
Hey,
it seems that the socket-module behaves differently on unix / windows when a timeout is set.
Here an example:
# test.py
import socket...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |