Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

trying sockets

0 views
Skip to first unread message

Uwe Schmitt

unread,
Oct 2, 2001, 9:07:16 AM10/2/01
to
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.S...@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

Carlos Gaston Alvarez

unread,
Oct 2, 2001, 9:54:41 AM10/2/01
to pytho...@python.org
There are http specific classes. Why dont you use them?

Chau,

Gaston

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


Mark Berry

unread,
Oct 2, 2001, 10:03:20 AM10/2/01
to
Without trying to write the code for you, basically your binding a
local socket and trying to serve connections, not just connect to a
remote socket. ;-)

they have pretty simple examples for doing this in the OReilly python
books, or probably any for that matter that covers sockets.

Good luck

Steve Holden

unread,
Oct 2, 2001, 10:30:38 AM10/2/01
to
"Uwe Schmitt" <u...@rocksport.de> 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 ???
>
Sockets are a little difficult to understand when you are just getting
started, but the good news is they are quite easy to deal with once you
understand the concepts involved. Gordon McMilllan has produced some very
useful tutorial material available in several places, including

http://www.mcmillan-inc.com/sock1.html

and I would encourage you to read that for an overview of the processes
involved.

The main problem is that you should use the same socket to send and receive
when you have established a connection to the server. Server structures are
rather different, you listen() on a socket, and when accept() returns it
provides you with a newly-created socket that you can use to communicate
with the connected client.

So you are actually making things much more difficult than they need to be!
Here's a revised program:

from socket import *
# Note that this is bad practice unless you KNOW
# the module is specifically designed for it.
# import socket
# snd = socket.socket(...)
# would be a more typical sequence.

snd=socket(AF_INET,SOCK_STREAM)
snd.connect(("www.num.uni-sb.de",80))
print snd.getsockname()

snd.send("GET / HTTP/1.0\r\n\r\n")
print snd.recv(1024)

snd.close()

There you go. When I run this it returns the following output:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>302 Found</TITLE>
</HEAD><BODY>
<H1>Found</H1>
The document has moved <A
HREF="http://www.num.uni-sb.de/iam/index.php">here</A>.<P>
<HR>
<ADDRESS>Apache/1.3.12 Server at caesar.num.uni-sb.de Port 80</ADDRESS>
</BODY></HTML>

This actually looks reasonable. Of course, there's lots more to do...

As another poster pointed out, Python does include libraries for client-side
and server-side HTTP, but they can be a little opaque at times. There's
nothing wrong with trying to hack up some simple scripts yourself, although
you will find there is much to be learned from the library code. Good luck!

regards
Steve
--
http://www.holdenweb.com/

Brian Lee

unread,
Oct 2, 2001, 10:05:40 AM10/2/01
to pytho...@python.org, Uwe Schmitt
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@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

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

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

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

Uwe Schmitt

unread,
Oct 2, 2001, 3:01:36 PM10/2/01
to
Steve Holden <sho...@holdenweb.com> wrote:

| This actually looks reasonable. Of course, there's lots more to do...

| As another poster pointed out, Python does include libraries for client-side
| and server-side HTTP, but they can be a little opaque at times. There's
| nothing wrong with trying to hack up some simple scripts yourself, although
| you will find there is much to be learned from the library code. Good luck!

thanks a lot. my aim was not primarly to download a web-page,
i just wanted to play with the socket-stuff. now i know how to
send via a socket.

0 new messages