trying sockets

Brian Lee senux at senux.com
Tue Oct 2 16:05:40 CEST 2001


Try urllib module. It's very easy to use to fetch web page.
And I think you don't need to bind in your source because
you want to fetch web page by GET command. You don't need
to start tcp server. Right?

[senux at pooky senux]$ python
Python 1.5.2 (#1, Mar  3 2001, 01:35:43)  [GCC 2.96 20000731 (Red Hat
Linux 7.1 2 on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import urllib
>>> a = urllib.urlopen('http://www.num.uni-sb.de')
>>> print len(a.read()) 
4678

I got 4678 bytes from www.num.uni-sb.de. 

Brian

On Tue, Oct 02, 2001 at 01:07:16PM +0000, Uwe Schmitt wrote:
> Hi,
> 
> i'm just playing with the socket module: i'd like to send
> a "GET/HTTP" request to a webserver and record the answer
> of the server. so i wrote
> 
>    from socket import *
> 
>    snd=socket(AF_INET,SOCK_STREAM)
>    snd.connect(("www.num.uni-sb.de",80))
>    print snd.getsockname()
> 
>    lstn=socket(AF_INET,SOCK_STREAM)
>    lstn.bind(snd.getsockname())
> 
>    snd.send("GET / HTTP/1.0 ")
>    lsten.listen(1)
>    conn,addr=lstn.accept()
>    print conn.recv(1024)
>  
>    lstn.close()
>    snd.close()
> 
> but i get the following output:
> 
>    ('134.96.31.42', 1662)
>    Traceback (most recent call last):
>       File "client.py", line 8, in ?
>         lsten.bind(snd.getsockname())
>    socket.error: (98, 'Address already in use')
> 
> so: what did i do wrong ???
> 
> yours, uwe.
>  
> 
> 
> -- 
> Uwe.Schmitt at num.uni-sb.de      Universität des Saarlandes
> phone: +49 (0)681/302-2468     Geb. 36.1, Zi. 4.17, PF 151150
>                                D-66041  Saarbrücken
> http://www.rocksport.de        http://www.rocksport.de/first_ride.mp3
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
Brian Lee <senux at senux.com> - http://www.senux.com/

Bigamy is having one spouse too many.  Monogamy is the same.




More information about the Python-list mailing list