How to connect to a website?

John Gordon gordon at panix.com
Mon Apr 22 12:38:47 EDT 2013


In <566767a8-35cc-47f2-9f75-032ce5629b44 at googlegroups.com> webmaster at terradon.nl writes:

> Hi,
> i just try to connect to a website, read that page and display the rules get from it.
> Then i get this error message:

>   File "D:/Python/Py projects/socket test/sockettest.py", line 21, in <module>
>     fileobj.write("GET "+filename+" HTTP/1.0\n\n")
> io.UnsupportedOperation: not writable

I haven't worked with the socket library, but I think this error is because
you specified a mode of 'r' when calling makefile().  fileobj is read-only,
and you're trying to write to it.

If you just want to connect to a website, try using the urllib2 module
instead of socket.  It's higher-level and handles a lot of details for
you.  Here's an example:

    import urllib2

    request = urllib2.Request('http://www.voidspace.org.uk')
    response = urllib2.urlopen(request)
    content = response.readlines()

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list