Dealing with Functions

This is the place for queries that don't fit in any of the other categories.

Dealing with Functions

Postby mettalmag » Tue Nov 26, 2013 5:46 am

Code: Select all
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 = '10.0.0.4'
    ip2 = '10.0.0.5'
    port = 21


banner1 = retBanner(ip1, port)


and in the last line were im trying to define banner1 i got the error that ip1 and port are not defined.
I'm confused, i'm trying to follow the tutorial but cant work it out.
Thx for your patience and help : )
Last edited by Mekire on Sat Nov 30, 2013 1:59 pm, edited 1 time in total.
Reason: First post lock.
User avatar
mettalmag
 
Posts: 2
Joined: Tue Nov 26, 2013 5:26 am

Re: Dealing with Functions

Postby stranac » Tue Nov 26, 2013 10:30 am

Well, ip1 and port are defined in the main() function.
They only exist inside that function.

Your last line is outside of the function.
So ip1 and port are not defined there.

In the future, please post your entire error traceback, instead of saying "i got the error that..."
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Dealing with Functions

Postby mettalmag » Sat Nov 30, 2013 11:06 am

Thanks for your reply
Code: Select all
>>> 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 = '10.0.0.4'
...     ip2 = '10.0.0.5'
...     port = 21
...
>>> banner1 = retBanner(ip1, port)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ip1' is not defined


this is the error i get,
I tried to put the kast line in def main(): but still got the error
how can i define them ?
User avatar
mettalmag
 
Posts: 2
Joined: Tue Nov 26, 2013 5:26 am

Re: Dealing with Functions

Postby stranac » Sat Nov 30, 2013 1:53 pm

mettalmag wrote:I tried to put the kast line in def main()

What's the kast line?

As I said, ip1 and port only exist inside the main() function.
One way you could fix the error is putting that last line inside the function as well.
But I can't be certain it's the correct solution, since I don't know what you want.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Dealing with Functions

Postby tnknepp » Tue Dec 03, 2013 2:01 pm

stranac wrote:
mettalmag wrote:I tried to put the kast line in def main()

What's the kast line?


I think he meant "last" line.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm

Re: Dealing with Functions

Postby stranac » Tue Dec 03, 2013 2:24 pm

Oh, that makes sense...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests