shaqywacky7 0 Newbie Poster

I made a program to search something on google and return how many results it has and it works perfectly as long as the sending code is not inside a function. If I put it in a function the reply from google isn't seen as a http packet(even though it has a source port of 80), it is seen as a stream of tcp. Normally wireshark would just turn the stream of tcp into a single http packet, and it does so long as I don't put the sending code in a function.

The only thing that I can think that may be causing the problem is that the function makes the computer not sure what program has that destination port. But I'm really unsure about that.

Heres the code:

import dpkt, dpkt.http, socket, pcap, thread, time

global control
control = 0

search_value = 'google'


def reader():          
  global control
  control = 0
  pc = pcap.pcap()
  pc.setfilter('ip')                           # This will filter out all packets that aren't IP(http will be carried by IP)
  for ts, pkt in pc:                          # Read the incomming packets
    print 'wh'
    eth = dpkt.ethernet.Ethernet(pkt)         #creates an object of the headers
    ip = eth.data                             # Strips off the ethernet header
    if ip.p == 6:                             # This checks the protocol header, if it is set to 6 that means it is carring tcp
      tcp = ip.data                           # Strips off the ip header
      if tcp.sport == 80:                     # Checks if the source port is 80, if it is then the packet is an http reply
        http = tcp.data                       # Strips off tcp header
        index = http.find('About ')           # Searches for the word "About " because About is the word diectly before the number of indexes
        fh = open('test.html', 'w')
        fh.write(http)
        print http
        
        if index != -1:                       # Makes sure the index is not out of range
          if ord(http[index+6]) > 47 and ord(http[index+6]) < 58:     # Checks if the character after "About " is a number
            index = index + 6                   # Moves the index up to the front of the number
            value = ''
            print 'http packet received'
            while http[index] != ' ':         #reads the characters and appends them to value until a space is encounted
              value = value + http[index]
              index += 1
            print "The google search for '" + search_value +"' has " + value + " results!"
#            control = 1
            break


def sender(search_value):

  ht = dpkt.http.Request()
  ht.method = 'GET'
  ht.uri = '/search?hl=en&source=hp&q='+search_value+'&aq=f&aqi=g10&aql=&oq=&gs_rfai=&fp=623bcfe6002bdef'

  ht.headers['host'] = 'www.google.com'
  ht.headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  ht.headers['Accept-Language'] = 'en-us,en;q=0.5'
  ht.headers['Accept-Charset'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
  ht.headers['Keep-Alive'] = '115'

  ht.body = 'test, sorry google.'

  addr = ('74.125.19.104', 80)

  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect(addr)

  sent = s.send(str(ht))


thread.start_new_thread((reader), ())             #Starts listening for packets

"""
ht = dpkt.http.Request()
ht.method = 'GET'
ht.uri = '/search?hl=en&source=hp&q='+search_value+'&aq=f&aqi=g10&aql=&oq=&gs_rfai=&fp=623bcfe6002bdef'

ht.headers['host'] = 'www.google.com'
ht.headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
ht.headers['Accept-Language'] = 'en-us,en;q=0.5'
ht.headers['Accept-Charset'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
ht.headers['Keep-Alive'] = '115'

ht.body = 'test, sorry google.'

addr = ('74.125.19.104', 80)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(addr)

sent = s.send(str(ht))

"""

sender(search_value)

while 1:
  if control == 1:
    break
  time.sleep(1)
  pass

Its set up to run the function, but if you want to see it work, just comment out the function and uncomment out the unfunctioned code.

Anyone seen this or know whats going on?