Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > asyncore.poll() question

Reply
Thread Tools

asyncore.poll() question

 
 
chad
Guest
Posts: n/a
 
      10-16-2010
At the following url..

http://www.nightmare.com/medusa/programming.html

The author has the following code for a simple HTTP client

#!/usr/bin/python

import asyncore
import socket
import string

class http_client (asyncore.dispatcher):

def __init__ (self, host, path):
asyncore.dispatcher.__init__ (self)
self.path = path
self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
self.connect ((host, 80))

def handle_connect (self):
self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)

def handle_read (self):
data = self.recv (8192)
print data

def handle_write (self):
pass

if __name__ == '__main__':
import sys
import urlparse
for url in sys.argv[1:]:
parts = urlparse.urlparse (url)
if parts[0] != 'http':
raise ValueError, "HTTP URL's only, please"
else:
host = parts[1]
path = parts[2]
http_client (host, path)
asyncore.loop()


Right after that, the author states the following...

" A really good way to understand select() is to put a print statement
into the asyncore.poll() function:

[...]
(r,w,e) = select.select (r,w,e, timeout)
print '---'
print 'read', r
print 'write', w
[...]

Each time through the loop you will see which channels have fired
which events.
"

How the heck do I modify the code put the print statement into the
asyncore.poll() function?

Chad
 
Reply With Quote
 
 
 
 
Lucasm
Guest
Posts: n/a
 
      10-16-2010
On 16 Okt, 15:31, chad <cdal...@gmail.com> wrote:
> At the following url..
>
> http://www.nightmare.com/medusa/programming.html
>
> The author has the following code for a simple HTTP client
>
> #!/usr/bin/python
>
> import asyncore
> import socket
> import string
>
> class http_client (asyncore.dispatcher):
>
> * * def __init__ (self, host, path):
> * * * * asyncore.dispatcher.__init__ (self)
> * * * * self.path = path
> * * * * self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
> * * * * self.connect ((host, 80))
>
> * * def handle_connect (self):
> * * * * self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)
>
> * * def handle_read (self):
> * * * * data = self.recv (8192)
> * * * * print data
>
> * * def handle_write (self):
> * * * * pass
>
> if __name__ == '__main__':
> * * import sys
> * * import urlparse
> * * for url in sys.argv[1:]:
> * * * * parts = urlparse.urlparse (url)
> * * * * if parts[0] != 'http':
> * * * * * * raise ValueError, "HTTP URL's only, please"
> * * * * else:
> * * * * * * host = parts[1]
> * * * * * * path = parts[2]
> * * * * * * http_client (host, path)
> * * asyncore.loop()
>
> Right after that, the author states the following...
>
> " A really good way to understand select() is to put a print statement
> into the asyncore.poll() function:
>
> * * * * [...]
> * * * * (r,w,e) = select.select (r,w,e, timeout)
> * * * * print '---'
> * * * * print 'read', r
> * * * * print 'write', w
> * * * * [...]
>
> Each time through the loop you will see which channels have fired
> which events.
> "
>
> How the heck do I modify the code put the print statement into the
> asyncore.poll() function?
>
> Chad


Hi,

You can find the file in your Python directory, in my case /usr/lib/
Python2.6/asyncore.py. You should delete the .pyc file to make sure it
is recompiled. And you will need root access .

Lucas
 
Reply With Quote
 
 
 
 
Felipe Bastos Nunes
Guest
Posts: n/a
 
      10-16-2010
Or download the old source files, and use the asyncore.py that's there.

2010/10/16, Lucasm <>:
> On 16 Okt, 15:31, chad <cdal...@gmail.com> wrote:
>> At the following url..
>>
>> http://www.nightmare.com/medusa/programming.html
>>
>> The author has the following code for a simple HTTP client
>>
>> #!/usr/bin/python
>>
>> import asyncore
>> import socket
>> import string
>>
>> class http_client (asyncore.dispatcher):
>>
>> * * def __init__ (self, host, path):
>> * * * * asyncore.dispatcher.__init__ (self)
>> * * * * self.path = path
>> * * * * self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
>> * * * * self.connect ((host, 80))
>>
>> * * def handle_connect (self):
>> * * * * self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)
>>
>> * * def handle_read (self):
>> * * * * data = self.recv (8192)
>> * * * * print data
>>
>> * * def handle_write (self):
>> * * * * pass
>>
>> if __name__ == '__main__':
>> * * import sys
>> * * import urlparse
>> * * for url in sys.argv[1:]:
>> * * * * parts = urlparse.urlparse (url)
>> * * * * if parts[0] != 'http':
>> * * * * * * raise ValueError, "HTTP URL's only, please"
>> * * * * else:
>> * * * * * * host = parts[1]
>> * * * * * * path = parts[2]
>> * * * * * * http_client (host, path)
>> * * asyncore.loop()
>>
>> Right after that, the author states the following...
>>
>> " A really good way to understand select() is to put a print statement
>> into the asyncore.poll() function:
>>
>> * * * * [...]
>> * * * * (r,w,e) = select.select (r,w,e, timeout)
>> * * * * print '---'
>> * * * * print 'read', r
>> * * * * print 'write', w
>> * * * * [...]
>>
>> Each time through the loop you will see which channels have fired
>> which events.
>> "
>>
>> How the heck do I modify the code put the print statement into the
>> asyncore.poll() function?
>>
>> Chad

>
> Hi,
>
> You can find the file in your Python directory, in my case /usr/lib/
> Python2.6/asyncore.py. You should delete the .pyc file to make sure it
> is recompiled. And you will need root access .
>
> Lucas
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Felipe Bastos Nunes
 
Reply With Quote
 
chad
Guest
Posts: n/a
 
      10-16-2010
On Oct 16, 6:47*am, Lucasm <lordlucr...@gmail.com> wrote:
> On 16 Okt, 15:31, chad <cdal...@gmail.com> wrote:
>
>
>
> > At the following url..

>
> >http://www.nightmare.com/medusa/programming.html

>
> > The author has the following code for a simple HTTP client

>
> > #!/usr/bin/python

>
> > import asyncore
> > import socket
> > import string

>
> > class http_client (asyncore.dispatcher):

>
> > * * def __init__ (self, host, path):
> > * * * * asyncore.dispatcher.__init__ (self)
> > * * * * self.path = path
> > * * * * self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
> > * * * * self.connect ((host, 80))

>
> > * * def handle_connect (self):
> > * * * * self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)

>
> > * * def handle_read (self):
> > * * * * data = self.recv (8192)
> > * * * * print data

>
> > * * def handle_write (self):
> > * * * * pass

>
> > if __name__ == '__main__':
> > * * import sys
> > * * import urlparse
> > * * for url in sys.argv[1:]:
> > * * * * parts = urlparse.urlparse (url)
> > * * * * if parts[0] != 'http':
> > * * * * * * raise ValueError, "HTTP URL's only, please"
> > * * * * else:
> > * * * * * * host = parts[1]
> > * * * * * * path = parts[2]
> > * * * * * * http_client (host, path)
> > * * asyncore.loop()

>
> > Right after that, the author states the following...

>
> > " A really good way to understand select() is to put a print statement
> > into the asyncore.poll() function:

>
> > * * * * [...]
> > * * * * (r,w,e) = select.select (r,w,e, timeout)
> > * * * * print '---'
> > * * * * print 'read', r
> > * * * * print 'write', w
> > * * * * [...]

>
> > Each time through the loop you will see which channels have fired
> > which events.
> > "

>
> > How the heck do I modify the code put the print statement into the
> > asyncore.poll() function?

>
> > Chad

>
> Hi,
>
> You can find the file in your Python directory, in my case /usr/lib/
> Python2.6/asyncore.py. You should delete the .pyc file to make sure it
> is recompiled. And you will need root access .
>
> Lucas


I just did that...

[root@localhost python2.6]# ls -al asyncore.py
-rw-r--r-- 1 root root 19262 Oct 16 10:22 asyncore.py
[root@localhost python2.6]# ls -al asyncore.pyc
-rw-r--r-- 1 root root 16773 Oct 16 10:26 asyncore.pyc
[root@localhost python2.6]# ls -al asyncore.pyo
-rw-r--r-- 1 root root 16773 Oct 16 10:42 asyncore.pyo
[root@localhost python2.6]#


And nothing happened. Ideas?
 
Reply With Quote
 
Felipe Bastos Nunes
Guest
Posts: n/a
 
      10-16-2010
You edited the source of asyncore.py puttin the print statments and
nothing happend? It should work as the method is called as the page
you posted said.

2010/10/16, chad <>:
> On Oct 16, 6:47*am, Lucasm <lordlucr...@gmail.com> wrote:
>> On 16 Okt, 15:31, chad <cdal...@gmail.com> wrote:
>>
>>
>>
>> > At the following url..

>>
>> >http://www.nightmare.com/medusa/programming.html

>>
>> > The author has the following code for a simple HTTP client

>>
>> > #!/usr/bin/python

>>
>> > import asyncore
>> > import socket
>> > import string

>>
>> > class http_client (asyncore.dispatcher):

>>
>> > * * def __init__ (self, host, path):
>> > * * * * asyncore.dispatcher.__init__ (self)
>> > * * * * self.path = path
>> > * * * * self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
>> > * * * * self.connect ((host, 80))

>>
>> > * * def handle_connect (self):
>> > * * * * self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)

>>
>> > * * def handle_read (self):
>> > * * * * data = self.recv (8192)
>> > * * * * print data

>>
>> > * * def handle_write (self):
>> > * * * * pass

>>
>> > if __name__ == '__main__':
>> > * * import sys
>> > * * import urlparse
>> > * * for url in sys.argv[1:]:
>> > * * * * parts = urlparse.urlparse (url)
>> > * * * * if parts[0] != 'http':
>> > * * * * * * raise ValueError, "HTTP URL's only, please"
>> > * * * * else:
>> > * * * * * * host = parts[1]
>> > * * * * * * path = parts[2]
>> > * * * * * * http_client (host, path)
>> > * * asyncore.loop()

>>
>> > Right after that, the author states the following...

>>
>> > " A really good way to understand select() is to put a print statement
>> > into the asyncore.poll() function:

>>
>> > * * * * [...]
>> > * * * * (r,w,e) = select.select (r,w,e, timeout)
>> > * * * * print '---'
>> > * * * * print 'read', r
>> > * * * * print 'write', w
>> > * * * * [...]

>>
>> > Each time through the loop you will see which channels have fired
>> > which events.
>> > "

>>
>> > How the heck do I modify the code put the print statement into the
>> > asyncore.poll() function?

>>
>> > Chad

>>
>> Hi,
>>
>> You can find the file in your Python directory, in my case /usr/lib/
>> Python2.6/asyncore.py. You should delete the .pyc file to make sure it
>> is recompiled. And you will need root access .
>>
>> Lucas

>
> I just did that...
>
> [root@localhost python2.6]# ls -al asyncore.py
> -rw-r--r-- 1 root root 19262 Oct 16 10:22 asyncore.py
> [root@localhost python2.6]# ls -al asyncore.pyc
> -rw-r--r-- 1 root root 16773 Oct 16 10:26 asyncore.pyc
> [root@localhost python2.6]# ls -al asyncore.pyo
> -rw-r--r-- 1 root root 16773 Oct 16 10:42 asyncore.pyo
> [root@localhost python2.6]#
>
>
> And nothing happened. Ideas?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Felipe Bastos Nunes
 
Reply With Quote
 
chad
Guest
Posts: n/a
 
      10-16-2010
On Oct 16, 11:02*am, Felipe Bastos Nunes <felipe.bast...@gmail.com>
wrote:
> You edited the source of asyncore.py puttin the print statments and
> nothing happend? It should work as the method is called as the page
> you posted said.
>
> 2010/10/16, chad <cdal...@gmail.com>:
>
>
>
> > On Oct 16, 6:47*am, Lucasm <lordlucr...@gmail.com> wrote:
> >> On 16 Okt, 15:31, chad <cdal...@gmail.com> wrote:

>
> >> > At the following url..

>
> >> >http://www.nightmare.com/medusa/programming.html

>
> >> > The author has the following code for a simple HTTP client

>
> >> > #!/usr/bin/python

>
> >> > import asyncore
> >> > import socket
> >> > import string

>
> >> > class http_client (asyncore.dispatcher):

>
> >> > * * def __init__ (self, host, path):
> >> > * * * * asyncore.dispatcher.__init__ (self)
> >> > * * * * self.path = path
> >> > * * * * self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
> >> > * * * * self.connect ((host, 80))

>
> >> > * * def handle_connect (self):
> >> > * * * * self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)

>
> >> > * * def handle_read (self):
> >> > * * * * data = self.recv (8192)
> >> > * * * * print data

>
> >> > * * def handle_write (self):
> >> > * * * * pass

>
> >> > if __name__ == '__main__':
> >> > * * import sys
> >> > * * import urlparse
> >> > * * for url in sys.argv[1:]:
> >> > * * * * parts = urlparse.urlparse (url)
> >> > * * * * if parts[0] != 'http':
> >> > * * * * * * raise ValueError, "HTTP URL's only, please"
> >> > * * * * else:
> >> > * * * * * * host = parts[1]
> >> > * * * * * * path = parts[2]
> >> > * * * * * * http_client (host, path)
> >> > * * asyncore.loop()

>
> >> > Right after that, the author states the following...

>
> >> > " A really good way to understand select() is to put a print statement
> >> > into the asyncore.poll() function:

>
> >> > * * * * [...]
> >> > * * * * (r,w,e) = select.select (r,w,e, timeout)
> >> > * * * * print '---'
> >> > * * * * print 'read', r
> >> > * * * * print 'write', w
> >> > * * * * [...]

>
> >> > Each time through the loop you will see which channels have fired
> >> > which events.
> >> > "

>
> >> > How the heck do I modify the code put the print statement into the
> >> > asyncore.poll() function?

>
> >> > Chad

>
> >> Hi,

>
> >> You can find the file in your Python directory, in my case /usr/lib/
> >> Python2.6/asyncore.py. You should delete the .pyc file to make sure it
> >> is recompiled. And you will need root access .

>
> >> Lucas

>
> > I just did that...

>
> > [root@localhost python2.6]# ls -al asyncore.py
> > -rw-r--r-- 1 root root 19262 Oct 16 10:22 asyncore.py
> > [root@localhost python2.6]# ls -al asyncore.pyc
> > -rw-r--r-- 1 root root 16773 Oct 16 10:26 asyncore.pyc
> > [root@localhost python2.6]# ls -al asyncore.pyo
> > -rw-r--r-- 1 root root 16773 Oct 16 10:42 asyncore.pyo
> > [root@localhost python2.6]#

>
> > And nothing happened. Ideas?


I edited the wrong file. Now it works.
 
Reply With Quote
 
chad
Guest
Posts: n/a
 
      10-17-2010
On Oct 16, 11:02*am, Felipe Bastos Nunes <felipe.bast...@gmail.com>
wrote:
> You edited the source of asyncore.py puttin the print statments and
> nothing happend? It should work as the method is called as the page
> you posted said.
>
> 2010/10/16, chad <cdal...@gmail.com>:
>
>
>
> > On Oct 16, 6:47*am, Lucasm <lordlucr...@gmail.com> wrote:
> >> On 16 Okt, 15:31, chad <cdal...@gmail.com> wrote:

>
> >> > At the following url..

>
> >> >http://www.nightmare.com/medusa/programming.html

>
> >> > The author has the following code for a simple HTTP client

>
> >> > #!/usr/bin/python

>
> >> > import asyncore
> >> > import socket
> >> > import string

>
> >> > class http_client (asyncore.dispatcher):

>
> >> > * * def __init__ (self, host, path):
> >> > * * * * asyncore.dispatcher.__init__ (self)
> >> > * * * * self.path = path
> >> > * * * * self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
> >> > * * * * self.connect ((host, 80))

>
> >> > * * def handle_connect (self):
> >> > * * * * self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)

>
> >> > * * def handle_read (self):
> >> > * * * * data = self.recv (8192)
> >> > * * * * print data

>
> >> > * * def handle_write (self):
> >> > * * * * pass

>
> >> > if __name__ == '__main__':
> >> > * * import sys
> >> > * * import urlparse
> >> > * * for url in sys.argv[1:]:
> >> > * * * * parts = urlparse.urlparse (url)
> >> > * * * * if parts[0] != 'http':
> >> > * * * * * * raise ValueError, "HTTP URL's only, please"
> >> > * * * * else:
> >> > * * * * * * host = parts[1]
> >> > * * * * * * path = parts[2]
> >> > * * * * * * http_client (host, path)
> >> > * * asyncore.loop()

>
> >> > Right after that, the author states the following...

>
> >> > " A really good way to understand select() is to put a print statement
> >> > into the asyncore.poll() function:

>
> >> > * * * * [...]
> >> > * * * * (r,w,e) = select.select (r,w,e, timeout)
> >> > * * * * print '---'
> >> > * * * * print 'read', r
> >> > * * * * print 'write', w
> >> > * * * * [...]

>
> >> > Each time through the loop you will see which channels have fired
> >> > which events.
> >> > "

>
> >> > How the heck do I modify the code put the print statement into the
> >> > asyncore.poll() function?

>
> >> > Chad

>
> >> Hi,

>
> >> You can find the file in your Python directory, in my case /usr/lib/
> >> Python2.6/asyncore.py. You should delete the .pyc file to make sure it
> >> is recompiled. And you will need root access .

>
> >> Lucas

>
> > I just did that...

>
> > [root@localhost python2.6]# ls -al asyncore.py
> > -rw-r--r-- 1 root root 19262 Oct 16 10:22 asyncore.py
> > [root@localhost python2.6]# ls -al asyncore.pyc
> > -rw-r--r-- 1 root root 16773 Oct 16 10:26 asyncore.pyc
> > [root@localhost python2.6]# ls -al asyncore.pyo
> > -rw-r--r-- 1 root root 16773 Oct 16 10:42 asyncore.pyo
> > [root@localhost python2.6]#

>
> > And nothing happened. Ideas?
> > --


One last question.

Why does the author have both import sys and import urlparse below
__main__? Ie

if __name__ == '__main__':
import sys
import urlparse

Why not just put them at the top with the rest?

Chad

 
Reply With Quote
 
Von
Guest
Posts: n/a
 
      10-17-2010
The urlparse module is load only when this module run as main entry.
Its for test purpose of modules.


2010/10/17, chad <>:
> On Oct 16, 11:02*am, Felipe Bastos Nunes <felipe.bast...@gmail.com>
> wrote:
>> You edited the source of asyncore.py puttin the print statments and
>> nothing happend? It should work as the method is called as the page
>> you posted said.
>>
>> 2010/10/16, chad <cdal...@gmail.com>:
>>
>>
>>
>> > On Oct 16, 6:47*am, Lucasm <lordlucr...@gmail.com> wrote:
>> >> On 16 Okt, 15:31, chad <cdal...@gmail.com> wrote:

>>
>> >> > At the following url..

>>
>> >> >http://www.nightmare.com/medusa/programming.html

>>
>> >> > The author has the following code for a simple HTTP client

>>
>> >> > #!/usr/bin/python

>>
>> >> > import asyncore
>> >> > import socket
>> >> > import string

>>
>> >> > class http_client (asyncore.dispatcher):

>>
>> >> > * * def __init__ (self, host, path):
>> >> > * * * * asyncore.dispatcher.__init__ (self)
>> >> > * * * * self.path = path
>> >> > * * * * self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
>> >> > * * * * self.connect ((host, 80))

>>
>> >> > * * def handle_connect (self):
>> >> > * * * * self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)

>>
>> >> > * * def handle_read (self):
>> >> > * * * * data = self.recv (8192)
>> >> > * * * * print data

>>
>> >> > * * def handle_write (self):
>> >> > * * * * pass

>>
>> >> > if __name__ == '__main__':
>> >> > * * import sys
>> >> > * * import urlparse
>> >> > * * for url in sys.argv[1:]:
>> >> > * * * * parts = urlparse.urlparse (url)
>> >> > * * * * if parts[0] != 'http':
>> >> > * * * * * * raise ValueError, "HTTP URL's only, please"
>> >> > * * * * else:
>> >> > * * * * * * host = parts[1]
>> >> > * * * * * * path = parts[2]
>> >> > * * * * * * http_client (host, path)
>> >> > * * asyncore.loop()

>>
>> >> > Right after that, the author states the following...

>>
>> >> > " A really good way to understand select() is to put a print
>> >> > statement
>> >> > into the asyncore.poll() function:

>>
>> >> > * * * * [...]
>> >> > * * * * (r,w,e) = select.select (r,w,e, timeout)
>> >> > * * * * print '---'
>> >> > * * * * print 'read', r
>> >> > * * * * print 'write', w
>> >> > * * * * [...]

>>
>> >> > Each time through the loop you will see which channels have fired
>> >> > which events.
>> >> > "

>>
>> >> > How the heck do I modify the code put the print statement into the
>> >> > asyncore.poll() function?

>>
>> >> > Chad

>>
>> >> Hi,

>>
>> >> You can find the file in your Python directory, in my case /usr/lib/
>> >> Python2.6/asyncore.py. You should delete the .pyc file to make sure it
>> >> is recompiled. And you will need root access .

>>
>> >> Lucas

>>
>> > I just did that...

>>
>> > [root@localhost python2.6]# ls -al asyncore.py
>> > -rw-r--r-- 1 root root 19262 Oct 16 10:22 asyncore.py
>> > [root@localhost python2.6]# ls -al asyncore.pyc
>> > -rw-r--r-- 1 root root 16773 Oct 16 10:26 asyncore.pyc
>> > [root@localhost python2.6]# ls -al asyncore.pyo
>> > -rw-r--r-- 1 root root 16773 Oct 16 10:42 asyncore.pyo
>> > [root@localhost python2.6]#

>>
>> > And nothing happened. Ideas?
>> > --

>
> One last question.
>
> Why does the author have both import sys and import urlparse below
> __main__? Ie
>
> if __name__ == '__main__':
> import sys
> import urlparse
>
> Why not just put them at the top with the rest?
>
> Chad
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57