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
Chau,
Gaston
> --
> http://mail.python.org/mailman/listinfo/python-list
>
they have pretty simple examples for doing this in the OReilly python
books, or probably any for that matter that covers sockets.
Good luck
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/
[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.
| 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.