-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport-scanner.py
60 lines (52 loc) · 1.67 KB
/
port-scanner.py
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
#!/usr/bin/python
import socket
import sys, errno
class Client:
def __init__(self,sock=None):
if sock is None:
self.sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock=sock
def connect(self, host, port):
return self.sock.connect_ex((host,port))
def mysend(self,msg):
MSGLEN = 1
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0 :
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
def myreceive(self):
chunks = []
bytes_recd = 0
try:
MSGLEN = int(self.sock.recv(1))
while bytes_recd < MSGLEN:
chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
if chunk == '':
raise RuntimeError("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
except:
print "Received bad Data"
return ''.join(chunks)
if __name__ == '__main__':
client = Client()
ports = []
if len(sys.argv) == 3:
server_address = sys.argv[1]
server_port = sys.argv[2]
else:
exit(errno.EINVAL)
client.connect(server_address,int(server_port))
tserver_port = client.myreceive()
while True:
tserver_port = client.myreceive()
if tserver_port == "0":
break
tclient = Client()
if(tclient.connect(server_address, int(tserver_port)) == 0):
print tserver_port
ports.append(tserver_port)
print ', '.join(ports)