星期三, 四月 26, 2006

python 实现一个http client

import sys
import socket
Host="10.2.5.240"
Port=80
mysocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM);
mysocket.connect((Host,Port))
#mysocket.send("HTTP/1.1\n\r");
mysocket.send("GET /ehrm/ HTTP/1.1\n\rHost:10.2.5.34\n\r\n\r");
length=0;
while 1:
text = mysocket.recv(2048)
if not text:
break
print text
length=length+len(text)
print "读取了",length,"字节"
mysocket.close();