在线等,初学python问个问题,高手请留步?(编译出错怎么回事?)
import socket, time
class MyClient:
def __init__(self):
print 'Prepare for connecting... '
def connect(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(( 'localhost ',8010))
sock.sendall( 'Hi, server ')
self.response = sock.recv(8192)
print 'Server: ',self.response
self.s = raw_input("Server: Do you want get the 'thinking in python ' file?(y/n):")
if self.s == 'y ':
while True:
self.name = raw_input( 'Server: input our name: ')
scok.sendall( 'name: ' + self.name.strop())
if self.response == 'valid ':
break
else:
print 'Server: Invalid username '
while True:
self.pwd = raw_input( 'Server: input our password: ')
self.sendall( 'pwd: ' + self.pwd.strip())
self.response = sock.recv(8192)
if self.response == 'valid ':
print 'please wait... '
f = open( 'b.pdf ', 'wb ')
while True:
data = sock.recv(1024)
if not data:
break
elif data == 'EOF ':
break
f.write(data)
f.flush()
f.close()
print 'download finished '
break
else:
print 'Server: Invalid password '
sock.sendall( 'bye ')
sock.close()
print 'Disconnected '
if __name__ == '__main__ ':
client = MyClient()
client.connect()
-》这个错误信息?
Traceback (most recent call last):
File "C:\Python25\Test1\client.py", line 53, in <module >
client.connect()
File "C:\Python25\Test1\client.py", line 10, in connect
sock.connect(( 'localhost ',8010))
File " <string >", line 1, in connect
error: (10061, 'Connection refused ')
还有这一句在这段代码当中有什么作用呢?
if __name__ == '__main__ ':
client = MyClient()
client.connect()
还有里面的self有什么作用呢?
[解决办法]
一、这个不是编译错误,是运行时异常。好像是你没启动server。google: 10061 Connection refused
二、self相当于C++中的this。
三、贴代码请用[code=Lang]...you code...[/code]格式。其中Lang处为Python。