-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconnection.py
More file actions
75 lines (60 loc) · 2.1 KB
/
Copy pathconnection.py
File metadata and controls
75 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/python
"""
connection.py contain Connection class
- Connection class
This class create a connection , connect, disconnect, send messages to a host
"""
import socket,time,sys
import threading
from counter import *
class Connection(threading.Thread):
options={"host":"192.168.1.6","port":80,"timebetweenrequest":1,"request":"GET / HTTP/1.0\r\n\r\n","response":"","debug":False,"counter":""}
def run(self):
con1=Connection()
con1.connect()
con1.sendMessage()
con1.getResponse()
def setOptions(self,options):
self.options=options.copy()
def setOption(self,option,value):
self.options[option]=value
def getOption(self,option):
return self.options[option]
def getOptions(self):
return self.options
def connect(self):
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(1)
try:
self.sock.connect((self.options['host'], self.options['port']))
if self.options['debug']:
print "[DEBUG] Connecting to %s:%s" %(self.options['host'], self.options['port'])
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(2)
def disconnect(self):
self.sock.close()
if self.options['debug']:
print "[DEBUG] Disconnecting from %s:%s" %(self.options['host'], self.options['port'])
def sendMessage(self):
if self.options['debug']:
print "[DEBUG] Sending data."
self.sock.send(self.options['request'])
self.setOption("counter",Counter())
self.getOption("counter").notify_alive()
def getResponse(self):
if self.options['debug']:
print "[DEBUG] Getting Slow data. "
receive_data=True
while receive_data:
try:
self.options['response']+=self.sock.recv(1)
self.sock.send(self.options['request'])
time.sleep(self.options['timebetweenrequest'])
except socket.error, e:
receive_data=False
self.getOption("counter").notify_death()
self.getOption("manager").newConnection(self.getOptions())