#!/usr/bin/env python
#

import subprocess
import threading
import os
import re
import sys
import time
import signal
import string
import random
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *

global ret

def http_request(http_type):
	host = "www.google.com"

	if http_type == '1.0':
	    get = 'GET /search?q=conntest HTTP/1.0\r\n'
	    reply = "HTTP/1.0 "
	else:
	    get = 'GET /search?q=conntest HTTP/1.1\r\nHost: ' + host + '\r\n\r\n'
            reply = "HTTP/1.1 "

	cmd = 'echo -e \"' + get + '\" | netcat ' + host + ' 80 2> /dev/null'

	try:
            ret = subprocess.check_output(cmd, shell=True).strip()
	except:
	    return False

	if string.find(ret, reply) != -1:
	    return True

	return False
	
def http_connection():
	global ret

	ret10 = http_request('1.0')	
	ret11 = http_request('1.1')

	if ret10 == True or ret11 == True:
	    ret = 1
	else:
	    ret = 0

	return

def main():
	global ret

	ret = None

	t = threading.Thread(target = http_connection)
        t.start()

	time.sleep(2)

	if ret == None:
	    subprocess.call("killall -SIGKILL netcat > /dev/null 2> /dev/null", shell=True)
	    time.sleep(2)
	
	    if ret == None:
		subprocess.call("killall -SIGKILL netcat > /dev/null 2> /dev/null", shell=True)
		time.sleep(1)

	if ret == None or ret == 0:
	    print "Status 0"
	    return 0

	print "Status 1"
	return 1

if __name__ == "__main__":
	main()
