Fastcode

Tons of GPL software codes

Found 10 results for network

0 notes

Python 2.7 irc hello world

# Python connect to an IRC server+channel
# Once connected to IRC it will say the message 'Hello!'
# 
# Internet Relay Chat (IRC) is a protocol for live interactive Internet text messaging (chat) or synchronous conferencing.[1] It 
# is mainly designed for group communication in discussion forums, called channels,[2] but also allows one-to-one communication 
# via private message[3] as well as chat and data transfer,[4] including file sharing.[5]
#
# IRC was created in 1988. Client software is available for every major operating system that supports Internet access.[6] As of 
# April 2011, the top 100 IRC networks served more than half a million users at a time,[7] with hundreds of thousands of 
# channels[7] operating on a total of roughly 1,500 servers[7] out of roughly 3,200 servers worldwide
#
# If you are new to IRC, i suggest first getting an IRC client such as XChat, BitchX or reading
# http://en.wikipedia.org/wiki/IRC
#
# Nitoshi

import socket
from time import gmtime, strftime
  
server = "irc.freenode.net"
channel = "#pythonsnake"
nickname = "PYguru"
  
def ping():
	ircsock.send("PONG :pingis\r\n")
  
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))
ircsock.send("NICK %s \r\n" % (nickname))
ircsock.send("USER %s %s %s :Ssssh\r\n" % (nickname,nickname,nickname))

ircsock.send("JOIN "+ channel +"\r\n")
ircsock.send("PRIVMSG "+ channel +" Hello! \r\n")  
  
while True:
	ircmsg = ircsock.recv(2048)
	ircmsg = ircmsg.strip('\n\r')
  
	if ircmsg.find("PING :") != -1:
		ping()
	
	


Filed under python irc network

0 notes

Python 2.7 port scan

import socket

HOST = '127.0.0.1'  
PORT = 6667              

print "-------------------------------"
print "Scan for open ports in Python"
print "-------------------------------"

for PORT in range(0,65536):
	try:
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.connect((HOST, PORT))
		print HOST + ":" + str(PORT) + " open"
		s.close()
	except:
		i = 1
		#print str(PORT) + " closed"

print "-------------------------------"

Filed under python security network

0 notes

Python 2.7 network server

import socket

HOST = ''         # localhost, i.e. your computer
PORT = 80         # port to connect to. usually 80 is set to HTTP/web
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()			# wait for connection
print 'Connection from ', addr		# print who connected
while 1:				
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()


Filed under python network socket

0 notes

Python 2.7 sockets connect to server

# This Python program connects to a server
# It prints out the first two messages it receives from that
# in this case, it should print
#
# Received ':calvino.freenode.net NOTICE * :*** Looking up your hostname...\r\n'
#
# Received ':calvino.freenode.net NOTICE * :*** Checking Ident\r\n:calvino.freenode.net NOTICE * :*** Found your hostname\r\n:calvino.freenode.net NOTICE * :*** 
# No Ident response\r\n'
#
# but the messages depends on the kind of server.

import socket

HOST = 'irc.freenode.net'  
PORT = 6667              
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

data = s.recv(1024)
print 'Received', repr(data) + "\n"

data = s.recv(1024)
print 'Received', repr(data) + "\n"

s.close()

Filed under python linux network

0 notes

Python 2.7 simple IRC bot

# Python connect to an IRC server+channel
# The program will answer the question 'How are you?'
#
# Internet Relay Chat (IRC) is a protocol for live interactive Internet text messaging (chat) or synchronous conferencing.[1] It 
# is mainly designed for group communication in discussion forums, called channels,[2] but also allows one-to-one communication 
# via private message[3] as well as chat and data transfer,[4] including file sharing.[5]
#
# IRC was created in 1988. Client software is available for every major operating system that supports Internet access.[6] As of 
# April 2011, the top 100 IRC networks served more than half a million users at a time,[7] with hundreds of thousands of 
# channels[7] operating on a total of roughly 1,500 servers[7] out of roughly 3,200 servers worldwide
#
# If you are new to IRC, i suggest first getting an IRC client such as XChat, BitchX or reading
# http://en.wikipedia.org/wiki/IRC
#
# Nitoshi

import socket
from time import gmtime, strftime
  
server = "irc.freenode.net"
channel = "#pythonsnake"
nickname = "Python4"
  
def ping():
	ircsock.send("PONG :pingis\r\n")
  
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))
ircsock.send("NICK %s \r\n" % (nickname))
ircsock.send("USER %s %s %s :Ssssh\r\n" % (nickname,nickname,nickname))

ircsock.send("JOIN "+ channel +"\r\n")
ircsock.send("PRIVMSG "+ channel +" Hello! \r\n")  
  
while True:
	ircmsg = ircsock.recv(2048)
	ircmsg = ircmsg.strip('\n\r')
  
	if ircmsg.find("how are you?") != -1:
	  ircsock.send("PRIVMSG "+ channel +" :Im" + " " + "great! \r\n")  
  
  
	if ircmsg.find("PING :") != -1:
		ping()
	
	


Filed under python network irc linux

0 notes

Python 2.7 web server code

# Simple Python network server HTTP/TCP
import socket

host = '127.0.0.1'
port = 80
conns = 5

# Start Socket Server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(conns)

print('Starting Python Server...\n')

# HTML Response
file = open('index.html','r')
html = file.read()
html = html.encode('utf-8')

# Listen for Connections
while True:
	conn, addr = s.accept()
	buff = conn.recv(1024)
	conn.send(html)
	
	if len(buff) > 0:
		print(buff.decode('utf-8'))

Filed under python programming network

0 notes

Python 2.7 get server info

import urllib
fp = urllib.urlopen("http://www.freebsd.org")

op = open("out.html", "wb")
n=0
while 1:
  s = fp.read(8192)
 
  if not s:
    break
  
  op.write(s)
  n = n + len(s)

fp.close()
op.close()

for k, v in fp.headers.items():
  print k, "=", v

print "copied", n, "bytes from", fp.url

Filed under python network

0 notes

Python 2.7 ping subnet and draw graph

import subprocess
import os
import threading
import networkx as nx
import matplotlib.pyplot as plt
import subprocess


i = 0
i_lock = threading.Lock()
mnetwork = []

def Ping():
  global i
  i_lock.acquire()
  try:
        i += 1
  finally:
        i_lock.release()

  ip = "192.168.1." + str(i)
  ping_response = subprocess.Popen(["/bin/ping", "-c4", "-w10", ip], stdout=subprocess.PIPE).stdout.read()

  #print ping_response
  if "Destination Host Unreachable" in ping_response:
    k = 0 # print ip + " : " + "offline"
  else:
    print ip + " : " + "online"
    mnetwork.append(ip)
   
threads = [threading.Thread(target=Ping) for t in range(255)]
for t in threads:
    t.start()

for t in threads:
    t.join()



print mnetwork



def draw_graph(graph, labels=None, graph_layout='shell',
               node_size=3300, node_color='blue', node_alpha=0.5, node_text_size=8,
               edge_color='blue', edge_alpha=0.1, edge_tickness=24, edges_pos=0.3,
               text_font='sans-serif'):

    # create networkx graph
    G=nx.Graph()

    # add edges
    for edge in graph:
        G.add_edge(edge[0], edge[1])

    # these are different layouts for the network you may try
    # shell seems to work best
    if graph_layout == 'spring':
        graph_pos=nx.spring_layout(G)
    elif graph_layout == 'spectral':
        graph_pos=nx.spectral_layout(G)
    elif graph_layout == 'random':
        graph_pos=nx.random_layout(G)
    else:
        graph_pos=nx.shell_layout(G)

    # draw graph
    nx.draw_networkx_nodes(G,graph_pos,node_size=node_size, alpha=node_alpha, node_color=node_color)
    nx.draw_networkx_edges(G,graph_pos,width=edge_tickness,alpha=edge_alpha,edge_color=edge_color)
    nx.draw_networkx_labels(G, graph_pos,font_size=node_text_size,font_family=text_font)

    if labels is not None:
        edge_labels = dict(zip(graph, labels))
        nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=edge_labels, label_pos=edges_pos)

    # show graph
    plt.show()


graph = []

for i in range(0,len(mnetwork)):
  graph.append( (mnetwork[i],mnetwork[i]) )

#graph = [(20, 21),(21, 22),(22, 23), (23, 24),(24, 25), (25, 20)]
draw_graph(graph)

Filed under python network

0 notes

Python ping subnet

Python 2.7 ping subnet

import subprocess
import os
import threading

i = 0
i_lock = threading.Lock()
network = []

def Ping():
  global i
  i_lock.acquire()
  try:
        i += 1
  finally:
        i_lock.release()

  ip = "192.168.1." + str(i)
  ping_response = subprocess.Popen(["/bin/ping", "-c4", "-w10", ip], stdout=subprocess.PIPE).stdout.read()

  #print ping_response
  if "Destination Host Unreachable" in ping_response:
    k = 0 # print ip + " : " + "offline"
  else:
    print ip + " : " + "online"
    
threads = [threading.Thread(target=Ping) for t in range(255)]
for t in threads:
    t.start()

for t in threads:
    t.join()

Filed under python network