Skip to content

Instantly share code, notes, and snippets.

@Lyude
Forked from marlyn-x86/sockets.py
Created May 13, 2016 00:49
Show Gist options
  • Save Lyude/9d17037176fc1a02a6dfe6a15def159d to your computer and use it in GitHub Desktop.
Save Lyude/9d17037176fc1a02a6dfe6a15def159d to your computer and use it in GitHub Desktop.
A thing to show packet information - currently, the notebook on the left doesn't resize nicely
import socket
from socket import (getaddrinfo)
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
url = "www.example.com"
port = 80
protocols = {
"AH" : socket.IPPROTO_AH,
"DSTOPTS" : socket.IPPROTO_DSTOPTS,
"EGP" : socket.IPPROTO_EGP,
"ESP" : socket.IPPROTO_ESP,
"FRAGMENT" : socket.IPPROTO_FRAGMENT,
"GRE" : socket.IPPROTO_GRE,
"HOPOPTS" : socket.IPPROTO_HOPOPTS,
"ICMP" : socket.IPPROTO_ICMP,
"ICMPV6" : socket.IPPROTO_ICMPV6,
"IDP" : socket.IPPROTO_IDP,
"IGMP" : socket.IPPROTO_IGMP,
"IP" : socket.IPPROTO_IP,
"IPIP" : socket.IPPROTO_IPIP,
"IPV6" : socket.IPPROTO_IPV6,
"NONE" : socket.IPPROTO_NONE,
"PIM" : socket.IPPROTO_PIM,
"PUP" : socket.IPPROTO_PUP,
"RAW" : socket.IPPROTO_RAW,
"ROUTING" : socket.IPPROTO_ROUTING,
"RSVP" : socket.IPPROTO_RSVP,
"SCTP" : socket.IPPROTO_SCTP,
"TCP" : socket.IPPROTO_TCP,
"TP" : socket.IPPROTO_TP,
"UDP" : socket.IPPROTO_UDP,
}
families = {
"APPLETALK" : socket.AF_APPLETALK,
"ASH" : socket.AF_ASH,
"ATMPVC" : socket.AF_ATMPVC,
"AX25" : socket.AF_AX25,
"BLUETOOTH" : socket.AF_BLUETOOTH,
"BRIDGE" : socket.AF_BRIDGE,
"CAN" : socket.AF_CAN,
"DECnet" : socket.AF_DECnet,
"ECONET" : socket.AF_ECONET,
"INET" : socket.AF_INET,
"INET6" : socket.AF_INET6,
"IPX" : socket.AF_IPX,
"IRDA" : socket.AF_IRDA,
"KEY" : socket.AF_KEY,
"LLC" : socket.AF_LLC,
"NETBEUI" : socket.AF_NETBEUI,
"NETLINK" : socket.AF_NETLINK,
"NETROM" : socket.AF_NETROM,
"PACKET" : socket.AF_PACKET,
"PPPOX" : socket.AF_PPPOX,
"RDS" : socket.AF_RDS,
"ROSE" : socket.AF_ROSE,
"ROUTE" : socket.AF_ROUTE,
"SECURITY" : socket.AF_SECURITY,
"SNA" : socket.AF_SNA,
"TIPC" : socket.AF_TIPC,
"UNIX" : socket.AF_UNIX,
"UNSPEC" : socket.AF_UNSPEC,
"WANPIPE" : socket.AF_WANPIPE,
"X25" : socket.AF_X25,
}
socket_types = {
"CLOEXEC" : socket.SOCK_CLOEXEC,
"DGRAM" : socket.SOCK_DGRAM,
"NONBLOCK" : socket.SOCK_NONBLOCK,
"RAW" : socket.SOCK_RAW,
"SEQPACKET" : socket.SOCK_SEQPACKET,
"STREAM" : socket.SOCK_STREAM,
}
def nb_recheck(notebook):
print(notebook.get_size_request())
print("Something is being called!")
class SocketTesterWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Socket Tester")
self.set_default_size(600, 400)
self.init_layout()
self.init_inputs()
def init_layout(self):
self.vbox1 = Gtk.VBox()
self.add(self.vbox1)
self.hbox1 = Gtk.HBox()
# self.hbox2 = Gtk.HBox()
self.hpaned1 = Gtk.HPaned()
self.vbox1.pack_end(self.hbox1, False, True, 0) # Text entry fields and a button
self.vbox1.pack_end(self.hpaned1, True, True, 0)
# self.vbox1.pack_end(self.hbox2, True, True, 0)
self.hbox_url = Gtk.HBox()
self.hbox_port = Gtk.HBox()
self.hbox1.pack_start(self.hbox_url, True, True, 0)
self.hbox1.pack_start(self.hbox_port, True, True, 0)
self.nb_socket = Gtk.Notebook()
self.nb_socket.connect("check-resize", nb_recheck)
self.hpaned1.pack1(self.nb_socket)
def init_inputs(self):
self.gae_button = Gtk.Button("Get Address Info")
self.hbox1.pack_end(self.gae_button, True, True, 0)
self.gae_button.connect("clicked", gai_button_cb, self)
self.url_label = Gtk.Label("Url: ")
self.url_entry = Gtk.Entry()
self.hbox_url.pack_start(self.url_label, True, True, 0)
self.hbox_url.pack_end(self.url_entry, True, True, 0)
self.port_label = Gtk.Label("Port: ")
self.port_entry = Gtk.Entry()
self.hbox_port.pack_start(self.port_label, True, True, 0)
self.hbox_port.pack_end(self.port_entry, True, True, 0)
self.output_win = Gtk.TextView()
self.output_buf = self.output_win.get_buffer()
self.hpaned1.pack2(self.output_win)
self.renderer_text = Gtk.CellRendererText()
self.proto_list = Gtk.ListStore(str)
for proto in protocols.keys():
self.proto_list.append([proto])
self.proto_tree = Gtk.TreeView(model=self.proto_list)
self.proto_column = Gtk.TreeViewColumn("Protocol", self.renderer_text, text = 0)
self.proto_tree.append_column(self.proto_column)
self.nb_socket.append_page(self.proto_tree, Gtk.Label("Protocols"))
self.fam_list = Gtk.ListStore(str)
for family in families.keys():
self.fam_list.append([family])
self.fam_tree = Gtk.TreeView(model = self.fam_list)
self.fam_column = Gtk.TreeViewColumn("Family", self.renderer_text, text = 0)
self.fam_tree.append_column(self.fam_column)
self.nb_socket.append_page(self.fam_tree, Gtk.Label("Families"))
self.typ_list = Gtk.ListStore(str)
for typ in socket_types.keys():
self.typ_list.append([typ])
self.typ_tree = Gtk.TreeView(model = self.typ_list)
self.typ_column = Gtk.TreeViewColumn("Type", self.renderer_text, text = 0)
self.typ_tree.append_column(self.typ_column)
self.nb_socket.append_page(self.typ_tree, Gtk.Label("Types"))
def show_message(self, text):
out_iter = self.output_buf.get_end_iter()
self.output_buf.insert(out_iter, "{}\n".format(text))
def gai_button_cb(button, self):
s_proto_t = get_selected_thing(self.proto_tree, self.proto_list)
s_proto = protocols[s_proto_t]
fam_t = get_selected_thing(self.fam_tree, self.fam_list)
fam = families[fam_t]
s_typ_t = get_selected_thing(self.typ_tree, self.typ_list)
s_typ = socket_types[s_typ_t]
url = self.url_entry.get_text()
port = int(self.port_entry.get_text())
self.show_message("Family: {}, Type: {}, Proto: {}".format(fam_t, s_typ_t, s_proto_t))
try:
self.show_message("{}".format(getaddrinfo(url, port, family = fam, type = s_typ, proto = s_proto)))
except socket.gaierror as e:
self.show_message("{}".format(e))
def get_selected_thing(treewidget, tree_list):
sel = treewidget.get_selection().get_selected()[1]
index = tree_list.get_path(sel)
result = (tree_list[index])[0]
return result
def show_output(string):
self.output_iter = self.output_buf.get_start_iter()
out_buffer.insert(out_iter, string)
def offer_choice_from_dict(choices):
option_list = list(choices.keys())
option_list.sort()
return choices[offer_choice_from_list(option_list)]
def offer_choice_from_list(choices):
for i in range(len(choices)):
print("{} : {}".format(i, choices[i]))
choice = input("Choice: ")
index = int(choice)
result = choices[index]
return result
if __name__ == '__main__':
# print("Choose an address family!")
# s_af = offer_choice_from_dict(families)
# print("Choose a socket type!")
# s_tp = offer_choice_from_dict(socket_types)
# print("Choose a protocol!")
# s_proto = offer_choice_from_dict(protocols)
# s_addr = input("Enter a URL!")
# s_port = int(input("Enter a port!"))
# result = getaddrinfo(s_addr, s_port, family = s_af, type = s_tp, proto = s_proto)
# print(result)
win = SocketTesterWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment