1. Maciej Fijalkowski
  2. hack2

Commits

Maciej Fijalkowski  committed 7dbab76

some improvements

  • Participants
  • Parent commits 520c096
  • Branches default

Comments (0)

Files changed (1)

File asynchammer/asynchammer.py

View file
 from twisted.internet.endpoints import TCP4ClientEndpoint
 from twisted.internet.error import ConnectionDone
 
+"Connection: keep-alive"
+
 HTTP_REQ = """GET / HTTP/1.1
 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
 Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
 Accept-Encoding:sdch
 Accept-Language:en-US,en;q=0.8
-Connection:keep-alive
 Cookie:MMMM=%(cookie)s; EEEE=%(cookie)s, ZZZZ=%(cookie)s, XXXX=%(cookie)s
 Host:www.google.com
 User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.58 Safari/537.22
 
 """ % {'cookie': 'a' * 100}
 
+CL = len('Content-length: ')
+
 class Hammer(Protocol):
     def __init__(self, factory):
         self.factory = factory
+        self.count = 0
+        self.exp_cl = 0
+        self.cs = 0
     
     def writeRequest(self):
         self.transport.write(HTTP_REQ)
 
     def dataReceived(self, data):
-        pass
-
+        if self.exp_cl == 0:
+            start = data.find('Content-Length: ')
+            if start:
+                end = data.find('\n', start)
+                self.exp_cl = int(data[start + CL:end])
+        cs = data.find('\r\n\r\n')
+        if cs:
+            self.exp_cl = self.exp_cl + cs
+        self.count += len(data)
+        if self.count == self.exp_cl + 4:
+            self.transport.loseConnection()
+            
     def connectionLost(self, fail):
         if not fail.type == ConnectionDone:
             pass