1.TCP客户端
#AF_INET 使用标准的IPv4地址或者主机名
#SOCK_STREAM是一个客户端
import socket target_host = 'www.google.com' target_port = 80 client = socket.socket(socket.AF_INET,socket.SOCKET_STREAM) client.connect = ((target_host,target_port)) client.send('GET / HTTP/1.1\r\nHost:google.com\r\n\r\n') response = client.recv(4096) print response
2,TCP服务器
多线程TCP服务器
import socket import threading bind_ip = '0.0.0.0' bind_port = 9999 server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.bind((bind_ip,bind_port)) server.listen(5) print 'TOTO:%s:%d' % (bind_ip,bind_port)
def handle_client(client_socket)
request = client_socket,recv(1024)
print 'Received: %s' % request
client_socket.send('ACK')
client_socket.close()
while True:
client,addr = server.accept()
print 'Accept from: %s:%d'%(addr[0],addr[1])
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
3.UTP客户端
import socket target_host = '127.0.0.1' target_port = 80 cliect = socket.socket(socket,AF_INET,socket.SOCK_DGRAM) client = sendto('AAVVCC',(target_host,target_port)) data,addr = client.recvfrom(4096) print data
5.取代netcat
import sys import socket import getopt import thrading import subporcess #定义全局变量 listen = False commanf = False upload = False execute = "" target = "" upload_destination = "" port = 0 def usage(): print 'BHP Net Tool' print print "Usage:bhpnet.py -t target_host -p port" print "-l --listen - listen on [host]:[port] for incoming connections" print "-e --execute=file_to_run - execute the given file upon receiving a connection" print " -c --command -initialize a commang shell" print "-u --upload=destination - upon receiving connection upload a file and write to [destination]" print print