Hey guys, recently i started with socket programming in python and still doing it. And i decided to do a project, its a text based GUI with different options that are related to Networks, but ive had trouble with it looping back to the menu which the input is represented by ">>>" thats what i want to loop it back to. I'm thinking using a while loop to loop it back but when i tried it. The one option would just loop in a continuous loop. Any suggestions? Thanks
import socket, sys, urllib, urllib2, webbrowser
a = 0
print "Welcome!"
print "\nType 'help' or '/help' for a list of syntax commands"
command = raw_input("\n>>>")
if command == 'ip':
host = raw_input("Host name: ")
result = socket.gethostbyaddr("host")
print "Primary hostname:"
print " " + result[0]
print "\nAddresses:"
for item in result[2]:
print " " + item
print "\nDone"
elif command == 'web':
print "\nCreating socket for connection..."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("www.google.com", 80))
url = 'http://www.google.com'
webbrowser.open_new_tab(url + '/doc')
webbrowser.open_new(url)
print "\nComplete"
elif command == 'html':
link = raw_input("Type the desired website for page source: ")
def addGETdata(url, data):
return url + '?' + urllib.urlencode(data)
zipcode = 'S2S 7U8'
url = addGETdata('link',
[('query', zipcode)])
print "Using URL", url
req = urllib2.Request(url)
fd = urllib2.urlopen(req)
while 1:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)
if command == 'ip list':
hostlookup = raw_input("Host name for matches: ")
result = socket.getaddrinfo('hostlookup', None, 0, socket.SOCK_STREAM)
counter = 0
for item in result:
print "%-2d: %s" % (counter, item[4])
counter += 1
if command == 'socketpeer':
print "Creating socket...",
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "done."
print "Looking up port number...",
port = socket.getservbyname('http', 'tcp')
print "done."
print "Connecting to remote host on port %d..." % port,
s.connect(("www.java2s.com", port))
print "done."
print "Connected from", s.getsockname()
print "Connected to", s.getpeername()