Maciej Fijalkowski avatar Maciej Fijalkowski committed 7dbab76

some improvements

Comments (0)

Files changed (1)

asynchammer/asynchammer.py

 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.