From ac171e119512fbcbc638192d4957a7d09badfe3e Mon Sep 17 00:00:00 2001 From: Mari Wahl Date: Tue, 16 Dec 2014 12:58:22 -0500 Subject: [PATCH] socket scripts added --- .../socket/{netcat.py => netcat_awesome.py} | 15 ++--- Network_and_802.11/socket/netcat_simple.py | 36 ++++++++++++ Network_and_802.11/socket/tcp_client.py | 28 ++++++++++ Network_and_802.11/socket/tcp_server.py | 56 +++++++++++++++++++ Network_and_802.11/socket/udp_client.py | 31 ++++++++++ 5 files changed, 159 insertions(+), 7 deletions(-) rename Network_and_802.11/socket/{netcat.py => netcat_awesome.py} (65%) create mode 100644 Network_and_802.11/socket/netcat_simple.py create mode 100644 Network_and_802.11/socket/tcp_server.py create mode 100644 Network_and_802.11/socket/udp_client.py diff --git a/Network_and_802.11/socket/netcat.py b/Network_and_802.11/socket/netcat_awesome.py similarity index 65% rename from Network_and_802.11/socket/netcat.py rename to Network_and_802.11/socket/netcat_awesome.py index 28a8eda..91f62a8 100644 --- a/Network_and_802.11/socket/netcat.py +++ b/Network_and_802.11/socket/netcat_awesome.py @@ -5,18 +5,19 @@ __author__ = "bt3" import socket +# Definning constants PORT = 12345 HOSTNAME = '54.209.5.48' -def netcat(hostname, port, content): +def netcat(text_to_send): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((hostname, port)) - s.sendall(content) + s.connect(( HOSTNAME, PORT)) + s.sendall(text_to_send) s.shutdown(socket.SHUT_WR) - adata = [] + adata = [] while 1: data = s.recv(1024) if data == "": @@ -24,12 +25,12 @@ def netcat(hostname, port, content): adata.append(data) s.close() - return adata if __name__ == '__main__': - message = netcat(HOSTNAME, PORT, '')[1] - print message \ No newline at end of file + text_to_send = '' + text_recved = netcat(text_to_send)) + print text_recved[1] \ No newline at end of file diff --git a/Network_and_802.11/socket/netcat_simple.py b/Network_and_802.11/socket/netcat_simple.py new file mode 100644 index 0000000..91f62a8 --- /dev/null +++ b/Network_and_802.11/socket/netcat_simple.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +__author__ = "bt3" + + +import socket + +# Definning constants +PORT = 12345 +HOSTNAME = '54.209.5.48' + + +def netcat(text_to_send): + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(( HOSTNAME, PORT)) + s.sendall(text_to_send) + s.shutdown(socket.SHUT_WR) + + adata = [] + while 1: + data = s.recv(1024) + if data == "": + break + adata.append(data) + + s.close() + return adata + + + +if __name__ == '__main__': + + text_to_send = '' + text_recved = netcat(text_to_send)) + print text_recved[1] \ No newline at end of file diff --git a/Network_and_802.11/socket/tcp_client.py b/Network_and_802.11/socket/tcp_client.py index f183afc..e410333 100644 --- a/Network_and_802.11/socket/tcp_client.py +++ b/Network_and_802.11/socket/tcp_client.py @@ -4,3 +4,31 @@ __author__ = "bt3" import socket + +# Defining constants +HOST = 'localhost' +PORT = 9090 +DATA = 'GET / HTTP/1.1\r\nHost: google.com\r\n\r\n' + + +def tcp_client(): + + # Create a socket object + # AF_INET parameter: to use standard IPv4 address + # SOCK_STREAM: to indicate tcp client + client = socket.socket( socket.AF_INET, socket.SOCK_STREAM) + + # Connect the client + client.connect(( HOST, PORT )) + + # Send data + client.send(DATA) + + # Receive some data + response = client.recv(4096) + print response + + + +if __name__ == '__main__': + tcp_client() diff --git a/Network_and_802.11/socket/tcp_server.py b/Network_and_802.11/socket/tcp_server.py new file mode 100644 index 0000000..3a14141 --- /dev/null +++ b/Network_and_802.11/socket/tcp_server.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +__author__ = "bt3" + + +import socket +import threading + +# Defining constants +# The IP address and port we want the server to listen on +BIND_IP = '0.0.0.0' +BIND_PORT = 9090 + + +# Start a thread to handle client connection +def handle_client(client_socket): + + # Get data from client + request = client_socket.recv(1024) + print "[*] Received: " + request + + # Send back a packet + client_socket.send('ACK') + + client_socket.close() + + + +def tcp_server(): + + # Create a socket object (just like the client) + server = socket.socket( socket.AF_INET, socket.SOCK_STREAM) + + # Start listening + server.bind(( BIND_IP, BIND_PORT)) + + # the maximum backlog of connections is set to 5 + server.listen(5) + print"[*] Listening on %s:%d" % (BIND_IP, BIND_PORT) + + # putting the server in the loop to wait for incoming connections + while 1: + + # when a client connects, we receive the client socket (client variable) + # the connections variables go to the addr variable + client, addr = server.accept() + print "[*] Accepted connection from: %s:%d" %(addr[0], addr[1]) + + # create a thread object that points to our function + client_handler = threading.Thread(target=handle_client, args=(client,)) + client_handler.start() + + + +if __name__ == '__main__': + tcp_server() diff --git a/Network_and_802.11/socket/udp_client.py b/Network_and_802.11/socket/udp_client.py new file mode 100644 index 0000000..5dee390 --- /dev/null +++ b/Network_and_802.11/socket/udp_client.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +__author__ = "bt3" + + +import socket + +# Defining constants +HOST = '127.0.0.1' +PORT = 80 +DATA = 'AAABBBCCC' + + +def udp_client(): + + # Create a socket object + # AF_INET parameter: to use standard IPv4 address + # SOCK_DGRAM: to indicate udp client + client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM) + + # Send data + client.sendto(DATA, ( HOST, PORT )) + + # Receive some data + data, addr = client.recvfrom(4096) + print data, addr + + + +if __name__ == '__main__': + udp_client()