The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
errors
Discuss errors in the Python Programming forum on Dev Shed. errors Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 11th, 2003, 02:30 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
|
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
|

December 11th, 2003, 02:50 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
__________________
programming language development: www.netytan.com – Hula
|

December 11th, 2003, 02:57 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
hmm thats weird i tested it that way before and it didnt work! hmm thanx again life-saver 
|

December 11th, 2003, 03:14 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
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 )
|

December 11th, 2003, 03:33 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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.
|

December 11th, 2003, 03:43 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
heh?  why  btw.. can you tell me why my _str__ is not working.. arg it suppost to work
|

December 11th, 2003, 05:45 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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
|

December 11th, 2003, 05:53 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
and the string representation:
Code:
def __str__(self):
return self.connection.recv(1024)
con = myTCP("www.bbc.co.uk",80)
print con

|

December 11th, 2003, 06:07 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
|
ahh cool cool im assuming that connection is a variable
|

December 11th, 2003, 06:25 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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 = .....

|

December 11th, 2003, 08:43 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

December 11th, 2003, 08:50 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
|
yes and i reolized that .. just weird that i did it that way before but didnt work.. not it does.. thanx
|

December 11th, 2003, 02:58 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
xlordt,
Not the best code in the world but it shows a way you might synchronise the copy between to PC's. It is not something to build a site on
You should look into the socket exceptions rather than the slap-dash stuff I've put here. I would really consider doing proper ftp - because although that has more coding overhead it at least handles error situations - this code does not.
not so Grim 
|

December 11th, 2003, 03:00 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
|
files
|

December 11th, 2003, 05:34 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
thanx 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|