newbie problem: use socke lib to retrieve one web page:

Erik Price erikprice at mac.com
Thu Sep 5 17:51:09 CEST 2002


On Wednesday, September 4, 2002, at 11:53  PM, koko wrote:

> I write this to retrieve one web page using socket lib.
>
> # retrieve webpage
> import socket
> HOST='www.uic.edu' # remote host
> PORT=80     # remort port
> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> m="""GET /home/events.shtml HTTP/1.0
> From: hh at uic.edu
> User-Agent: test/1.0
> """
> s.connect((HOST,PORT))
> s.send(m)
> data=s.recv(4096)
> print data
> s.close()

Works for me:

 >>> import socket
 >>> host = 'www.uic.edu'
 >>> port = 80
 >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 >>> s.connect((host, port))
 >>> header = """HEAD /home/events.shtml HTTP/1.0
... From: hh at uic.edu
... User-Agent: test/1.0
...
... """
 >>> s.send(header)
72
 >>> data = s.recv(4096)
 >>> print data
HTTP/1.1 200 OK
Date: Thu, 05 Sep 2002 15:40:00 GMT
Server: Apache/1.3.26 (Unix) PHP/4.1.2 mod_perl/1.27 mod_ssl/2.8.10 
OpenSSL/0.9.6
Connection: close
Content-Type: text/html


 >>> s.close()


I used the HEAD method instead of GET for brevity.  But GET works too.



Erik






--
Erik Price

email: erikprice at mac.com
jabber: erikprice at jabber.org





More information about the Python-list mailing list