socket scripts added

This commit is contained in:
Mari Wahl 2014-12-16 12:58:22 -05:00
parent d01824f424
commit ac171e1195
5 changed files with 159 additions and 7 deletions

View File

@ -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
text_to_send = ''
text_recved = netcat(text_to_send))
print text_recved[1]

View File

@ -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]

View File

@ -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()

View File

@ -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()

View File

@ -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()