我使用python来分析在线广告系统。我尝试连接到服务器但失败了,我在终端中运行的代码模板是:python client.py username portnumber
,
这是服务器端的代码:
import sys
import time
import socket
TIMEOUT = 30
class Firefly:
def __init__(self, port):
self._port = port
def _get_the_dataz(self, s):
ret = ""
s.settimeout(TIMEOUT)
while True:
data = s.recv(4096)
if not data:
break
ret = ret + data;
return ret
def _send_command(self, command, retries=3):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.connect(('127.0.0.1', self._port))
s.connect(('localhost', self._port))
s.send(command)
ret = self._get_the_dataz(s)
s.close()
return ret
except socket.error as ex:
print "Firefly timeout, %d retries left" % retries
print str(ex)
if retries == 0:
print "Can't reach firefly on port %d!" % self._port
raise
retries -= 1
time.sleep([30, 10, 2][retries])
return self._send_command(command, retries)
def get_visual_elements(self, url):
return self._send_command('GOTO %s' % url)
它总是这样返回:
[errno 111] connection refused cannot reach server on port 80 traceback(most recent call last): file "server.py", line 27, in _send_command s.connect(('localhost', self._port)) file "server.py", line 224, in meth return getattr(self._sock,name)(*args)
然后我尝试“telnet 127.0.0.1 80”, 它返回:
无法连接到远程主机,连接被拒绝。
我该怎么办?