Simple HTTP client bug?

David Lees DavidL.nono.nospam at raqia.com
Thu Dec 7 15:08:10 EST 2000


I am trying to run the following simple HTTP client code, copied
directly from "Programming in Python with Medusa and the Async Sockets
Library", but I get the following error every time I run it (regardless
of what URL I use or whether I run under NT or Linux Redhat 6.2).  Can
someone tell me what I am doing wrong?

Thanks in advance.

david lees


>>> host,path= www.nightmare.com
starting asyncore loop
sending GET
GET  HTTP/1.0


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that
this server could not understand.<P>
Invalid URI in request GET  HTTP/1.0<P>
</BODY></HTML>

warning: unhandled close event



----
The code is:

# -*- Mode: Python; tab-width: 4 -*-

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):
        print 'sending GET'
        print 'GET %s HTTP/1.0\r\n\r\n' % self.path
        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]
            print 'host,path=',host,path
            http_client (host, path)
            print 'starting asyncore loop'
    asyncore.loop()



More information about the Python-list mailing list