Error: child process close a socket inherited from parent

2011-05-29 Thread narke
Hi,

As illustrated in the following simple sample:

import sys
import os
import socket

class Server:
def __init__(self):
self._listen_sock = None

def _talk_to_client(self, conn, addr):
text = 'The brown fox jumps over the lazy dog.\n'
while True:
conn.send(text)
data = conn.recv(1024)
if not data:
break
conn.close()

def listen(self, port):
self._listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._listen_sock.bind(('', port))
self._listen_sock.listen(128)
self._wait_conn()

def _wait_conn(self):
while True:
conn, addr = self._listen_sock.accept()
if os.fork() == 0:
self._listen_sock.close()   # line x
self._talk_to_client(conn, addr)
else:
conn.close()

if __name__ == '__main__':
Server().listen(int(sys.argv[1]))

Unless I comment out the line x, I will get a 'Bad file descriptor'
error when my tcp client program (e.g, telnet) closes the connection to
the server.  But as I understood, a child process can close a unused
socket (file descriptor).

Do you know what's wrong here?


-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

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


Re: Error: child process close a socket inherited from parent

2011-05-29 Thread narke
On 2011-05-29, narke  wrote:
> Hi,
>
> As illustrated in the following simple sample:
>
> import sys
> import os
> import socket
>
> class Server:
> def __init__(self):
> self._listen_sock = None
>
> def _talk_to_client(self, conn, addr):
> text = 'The brown fox jumps over the lazy dog.\n'
> while True:
> conn.send(text)
> data = conn.recv(1024)
> if not data:
> break
> conn.close()
>
> def listen(self, port):
> self._listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> self._listen_sock.bind(('', port))
> self._listen_sock.listen(128)
> self._wait_conn()
>
> def _wait_conn(self):
> while True:
> conn, addr = self._listen_sock.accept()
> if os.fork() == 0:
> self._listen_sock.close()   # line x
> self._talk_to_client(conn, addr)
> else:
> conn.close()
>
> if __name__ == '__main__':
> Server().listen(int(sys.argv[1]))
>
> Unless I comment out the line x, I will get a 'Bad file descriptor'
> error when my tcp client program (e.g, telnet) closes the connection to
> the server.  But as I understood, a child process can close a unused
> socket (file descriptor).
>
> Do you know what's wrong here?
>
>

I forgot to say, it's Python 2.6.4 running on linux 2.6.33


-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

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


Re: Error: child process close a socket inherited from parent

2011-05-29 Thread narke
On 2011-05-29, Chris Torek  wrote:
> In article 
> narke   wrote:
>>As illustrated in the following simple sample:
>>
>>import sys
>>import os
>>import socket
>>
>>class Server:
>>def __init__(self):
>>self._listen_sock = None
>>
>>def _talk_to_client(self, conn, addr):
>>text = 'The brown fox jumps over the lazy dog.\n'
>>while True:
>>conn.send(text)
>>data = conn.recv(1024)
>>if not data:
>>break
>>conn.close()
>>
>>def listen(self, port):
>>self._listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>self._listen_sock.bind(('', port))
>>self._listen_sock.listen(128)
>>self._wait_conn()
>>
>>def _wait_conn(self):
>>while True:
>>conn, addr = self._listen_sock.accept()
>>if os.fork() == 0:
>>self._listen_sock.close()   # line x
>>self._talk_to_client(conn, addr)
>>else:
>>conn.close()
>>
>>if __name__ == '__main__':
>>Server().listen(int(sys.argv[1]))
>>
>>Unless I comment out the line x, I will get a 'Bad file descriptor'
>>error when my tcp client program (e.g, telnet) closes the connection to
>>the server.  But as I understood, a child process can close a unused
>>socket (file descriptor).
>
> It can.
>
>>Do you know what's wrong here?
>
> The problem turns out to be fairly simple.
>
> The routine listen() forks, and the parent process (with nonzero pid)
> goes into the "else" branch of _wait_conn(), hence closes the newly
> accepted socket and goes back to waiting on the accept() call, which
> is all just fine.
>
> Meanwhile, the child (with pid == 0) calls close() on the listening
> socket and then calls self._talk_to_client().
>
> What happens when the client is done and closes his end?  Well,
> take a look at the code in _talk_to_client(): it reaches the
> "if not data" clause and breaks out of its loop, and calls close()
> on the accepted socket ... and then returns to its caller, which
> is _wait_conn().
>
> What does _wait_conn() do next?  It has finished "if" branch in
> the "while True:" loops, so it must skip the "else" branch and go
> around the loop again.  Which means its very next operation is
> to call accept() on the listening socket it closed just before
> it called self._talk_to_client().
>
> If that socket is closed, you get an EBADF error raised.  If not,
> the child and parent compete for the next incoming connection.


Chris,

 Thanks, you helped to find out a bug in my code. 
-- 
http://mail.python.org/mailman/listinfo/python-list


A ClientForm Question

2005-04-01 Thread narke
Does anyone here use ClientForm to handle a HTML form on client side?

I got a form, within which there is a image control, it direct me to
another page if i use mouse click on it.  the code of the form as
below:



...

 

...



So write below code to 'click' the image button,

forms = ParseResponse(urlopen(url))

form = forms[0]
urlopen(form.click("ZoomControl1:Imagebutton2"))

unfortunatly, however, when the code run, it just got a page which is
not the one i desired ( i actually wish to get the same page as i
'click' the button).  I guess that is "onclick=" statement cause
something weird, but I do not understand it.  And, in the source
containing the form, i found nowhere the Page_ClientValidate() resides.

What's wrong?

-
narke

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


Re: A ClientForm Question

2005-04-06 Thread narke
John J. Lee wrote,

> See second bullet point under "Why does .click()ing on a button not
work for me?".

Thanks for you advice. However, after read through the FAQs, I have not
managed to find a solution for my problem.  I belive my button is
coupled with some Java script which mess thing up and there is no a
easy solution.  Am I right?

-
narke

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


Distribute Non Library

2010-09-12 Thread narke
My simple tool writing in python get bigger and bigger and I think I'd
better split my code into several files.  But, unlike what in some other
languages, there is no way to compile these several files into a single
executable. Before I splitting my simple tool program, I just put it in
/usr/local/bin and run, very simple.  Now I have to consider distribute
a lot of files in a whole.  I know distutils can help, but I feel it is
a little uncomfortable, since I am not sharing a library, I am not
sharing any thing. I just want to refactory my code.  Is there a better
solution to my case?  Can I simply create a directory in /usr/local/bin
and put my script and other used files into the directory?  Does
distutils help in this case?

Thanks in advance.

-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke


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


Re: Distribute Non Library

2010-09-12 Thread narke
On 2010-09-12, Glazner  wrote:
> On Sep 12, 5:10 am, narke  wrote:
>> My simple tool writing in python get bigger and bigger and I think I'd
>> better split my code into several files.  But, unlike what in some other
>> languages, there is no way to compile these several files into a single
>> executable. Before I splitting my simple tool program, I just put it in
>> /usr/local/bin and run, very simple.  Now I have to consider distribute
>> a lot of files in a whole.  I know distutils can help, but I feel it is
>> a little uncomfortable, since I am not sharing a library, I am not
>> sharing any thing. I just want to refactory my code.  Is there a better
>> solution to my case?  Can I simply create a directory in /usr/local/bin
>> and put my script and other used files into the directory?  Does
>> distutils help in this case?
>>
>> Thanks in advance.
>
> try :
> python mayApp.zip
>
> myApp.zip <--> all your files + a __main__.py file as a starting
> point...

looks also not decent :(  i want my tool appear as an executabe, not an
zip.  but thank you anyway.


-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke


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


Re: Distribute Non Library

2010-09-12 Thread narke
On 2010-09-12, Diez B. Roggisch  wrote:
> narke  writes:
>
>> My simple tool writing in python get bigger and bigger and I think I'd
>> better split my code into several files.  But, unlike what in some other
>> languages, there is no way to compile these several files into a single
>> executable. Before I splitting my simple tool program, I just put it in
>> /usr/local/bin and run, very simple.  Now I have to consider distribute
>> a lot of files in a whole.  I know distutils can help, but I feel it is
>> a little uncomfortable, since I am not sharing a library, I am not
>> sharing any thing. I just want to refactory my code.  Is there a better
>> solution to my case?  Can I simply create a directory in /usr/local/bin
>> and put my script and other used files into the directory?  Does
>> distutils help in this case?
>
> Consider using setuptools + console entry points. With these, you will
> automatically generate a shell-script to execute for your otherwise
> eggified and easy installable package.
>
> Diez

That's really attracting.  I will try, thanks!

-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke
test post
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Distribute Non Library

2010-09-12 Thread narke
On 2010-09-12, CM  wrote:
> On Sep 11, 11:10 pm, narke  wrote:
>> My simple tool writing in python get bigger and bigger and I think I'd
>> better split my code into several files.  But, unlike what in some other
>> languages, there is no way to compile these several files into a single
>> executable.
>
> Sure there is.  py2exe, py2app, cx_freeze, etc.

Also good solution! Thanks for the help.  And, cx_freeze looks even
workable on my Linux.  Great.

-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke
test post
-- 
http://mail.python.org/mailman/listinfo/python-list


smtplib with Google

2010-09-13 Thread narke
Hi,

Can anyone please show me a workable example that can let me use
google's smtp server to send out a message?  Thanks.

-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke


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


Re: smtplib with Google

2010-09-13 Thread narke
On 2010-09-13, member thudfoo  wrote:
> On Mon, Sep 13, 2010 at 9:20 AM, narke  wrote:
>>
>> Hi,
>>
>> Can anyone please show me a workable example that can let me use
>> google's smtp server to send out a message?  Thanks.
>>
>
> Go here:
>
>   http://code.activestate.com/recipes/langs/python/
>
> and search for this
>
>  gmail

Thanks!

-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke


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