Calling function main()

Calling function main()

Postby coldhand » Mon May 05, 2014 4:13 pm

The error I get when I run the followiong script is "name ip1' is not defined". I don't understand the last two lines. Can sombody explain it to me:
if __name__ == '__main__':
main()
----------------------------
Code: Select all
#!/usr/bin/python
import socket
def retBanner(ip, port):
try:
  socket.setdefaulttimeout(2)
  s=socket.socket()
  s.connect((ip,port))
  banner=s.recv(1024)
  return banner
except:
  return
def main():
  ip1 = '192.168.233.128'
  port = 22
  banner1 = retBanner(ip1,port)
if banner1:
  print '[+]' + ip1 +':' + banner1
if __name__ == '__main__':
  main()
Last edited by coldhand on Mon May 05, 2014 4:50 pm, edited 2 times in total.
coldhand
 
Posts: 2
Joined: Mon May 05, 2014 4:01 pm

Re: Calling function main()

Postby metulburr » Mon May 05, 2014 4:33 pm

there is not point in using code tags if you do not maintain python's indentation anyways. Fix that.

Code: Select all
s.connect((ip,port))

ip used here is within retBanner scope. The assignemet to ip variable is within main's scope. Its hard to tell with yourr codes indentation all messed up.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1504
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Calling function main()

Postby coldhand » Mon May 05, 2014 4:55 pm

How do yo do code indentation?. Thanks.

I used spaces to create indetnation. I don't have any syntax error.

Can you explain why we use? >>> if __name__ == "__main__":main()
coldhand
 
Posts: 2
Joined: Mon May 05, 2014 4:01 pm

Re: Calling function main()

Postby lorddarthvader » Wed May 07, 2014 4:53 pm

the code will run perfectly even without it.
but it's done so that the program will run only if you execute it directly. if you're importing it, __name__ will be the name of the module.
--If at first you don't succeed, destroy all evidence that you tried.
My github
fite me
User avatar
lorddarthvader
 
Posts: 33
Joined: Mon Apr 07, 2014 11:38 am

Re: Calling function main()

Postby Marbelous » Fri May 09, 2014 7:13 pm

Marbelous
 
Posts: 154
Joined: Fri May 31, 2013 8:12 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest