import java.io.*;
import java.net.*;
public class Test {
public static void main(String[] args) throws IOException {
String query = "java";
Socket s = new Socket("google.com",80);
PrintStream p = new PrintStream(s.getOutputStream());
p.print("GET /search?q="+query+" HTTP/1.0\r\n");
p.print("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n");
p.print("Connection: close\r\n\r\n");
InputStreamReader in = new InputStreamReader(s.getInputStream());
BufferedReader buffer = new BufferedReader(in);
String line;
while ((line = buffer.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
The python code for this
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'http://www.google.com'
port = 80
dat = "GET /search?q=java HTTP/1.0\r\n User-Agent: Mozilla/5.0 \
(Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n\
Connection: close\r\n\r\n"
s.connect((host, port))
s.send(dat)
i = 0
while(1):
data = s.recv(1000)
i += 1
if (i < 5):
print data
if not data:
print 'received',len(data),'bytes'
s.close()