|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
errors
OK I'm trying to code new stuff in new ways for experience before i can get to code something good.. anyways.. here is my code
Code:
class TCP:
def __init__( self, host, port ):
self = socket( AF_INET, SOCK_STREAM )
self.connect(( "%s", "%d" )) % ( host, port )
self.send( 'GET /index.php/HTML/1.1 \n\r\n\r' )
print self.recv( 1025 )
if __name__ == '__main__':
print 'Enter host name to connect to'
print '> '
connection = raw_input( )
print 'Enter port number'
ports = raw_input( )
con = TCP( connection, ports )
and here is my error ![]() Code:
Traceback (most recent call last):
File "scripts/ftp.py", line 23, in ?
con = TCP( connection, ports )
File "scripts/ftp.py", line 9, in __init__
self.connect(( "%s", "%d" )) % ( host, port )
File "<string>", line 1, in connect
TypeError: an integer is required
can anyone tell me why i keep getting that err? i am providing an integer as a port number
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
||||
|
||||
|
But your not, you inserting an ind %d into a string, but the value is still a string
. Also add the fact that raw_input always returns a string and you have your problem i.e.>>> '%d' % 50 '50' >>> raw_input('Number please: ') Number please: 50 '50' >>> you could replace connect with something like this... ... self.connect((host, int(port))) ... Mark. |
|
#3
|
||||
|
||||
|
hmm thats weird i tested it that way before and it didnt work! hmm thanx again life-saver
![]() |
|
#4
|
||||
|
||||
|
btw how can this part work
Code:
class TCP:
def __init__( self, host, port ):
self = socket( AF_INET, SOCK_STREAM )
self.connect(( host, int( port )))
self.send( 'GET /index.php/HTML/1.1 \n\r\n\r' )
def __str__( self ):
print self.recv( 1025 )
|
|
#5
|
||||
|
||||
|
It's a "free" world. Do what you like but leave self alone or you'll regret it later. The run-time system will send the boys around
![]() Code:
>>> class fred:
def __init__(self):
print "It's me %s"%(self,)
>>> a = fred()
It's me <__main__.fred instance at 0x00A36328>
>>> a
<__main__.fred instance at 0x00A36328>
>>> a.self
Traceback (most recent call last):
File "<pyshell#9>", line 1, in -toplevel-
a.self
AttributeError: fred instance has no attribute 'self'
>>>
Grim. |
|
#6
|
||||
|
||||
|
heh?
why btw.. can you tell me why my _str__ is not working.. arg it suppost to work |
|
#7
|
||||
|
||||
|
Hi,
self is a psuedo variable created by the run-time system and acts as a pointer to the object instance. Code:
class MyClass: def __init__(self, number): self.apples = number a = MyClass(10) b = MyClass (3) print a.apples, b.apples self is a bit like like the this variable in C++. In the above example both a and b have an attribute called apples, they are both derived from the MyClass but the quantity of apples is unique to each. ![]() This code works for me: Code:
import socket
class myTCP:
def __init__(self,host,port):
print "Making connection..."
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((host,port))
self.connection.send("GET /index.html/HTML/1.1 \n\r\n\r")
print "Sent GET"
def output(self):
return self.connection.recv(1024)
con = myTCP("www.bbc.co.uk",80)
print con.output()
Grim |
|
#8
|
||||
|
||||
|
and the string representation:
Code:
def __str__(self):
return self.connection.recv(1024)
con = myTCP("www.bbc.co.uk",80)
print con
![]() |
|
#9
|
||||
|
||||
|
ahh cool cool im assuming that connection is a variable
|
|
#10
|
||||
|
||||
|
Yep!
self.connection is a variable (attribute) of the con instance of class MyTCP. I could of called it anything I liked of course: Code:
self.my_connection_to_the_server = ..... ![]() |
|
#11
|
||||
|
||||
|
Ok, i think i should mention that self is optional so you can use whatever name you like i.e. this... however Python standards are too use self so please do
![]() Also, Pythons style guide/standard is to NOT put spaces directly inside brackets i.e raw_input( 'something' ), its up to you whether you follow them though http://www.python.org/doc/essays/styleguide.html X: the reason you need to call int() on port is that connect takes a tuple of (host_as_string, port_as_int). raw_input() returns a string so you have to change its type of connect will barf! Mark. |
|
|