2016-08-14 74 views
0

我刚开始学习python。我刚刚到插座的章节和整个下面的代码来:用Python插座

import socket 
import sys 


HOST = '' 
PORT = 4444 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print "Our socket is created" 

try: 
    s.bind((HOST, PORT)) 
except socket.error as e: 
    print "Error in binding the port" 
    sys.exit() 
print "Binding is complete" 

s.listen(20) 
print "Server is in listen mode now!" 

while 1: 
    conn, addr = s.accept() 
    print "Connected with the : " + addr [0] + ' : ' +str(addr[1]) 
    s.close() 

当我运行它正常工作的代码,但是当我尝试连接到端口4444我在控制台中出现以下错误我想不通为什么:

我试图连接到端口4444本地主机使用腻子,我想SSH,telnet和生,他们都得到了同样的错误

Our socket is created 
Binding is complete 
Server is in listen mode now! 
Connected with the : 127.0.0.1 : 49278 
Traceback (most recent call last): 
    File "C:\pythontraining\Module12\server-working.py", line 24, in <module> 
    conn, addr = s.accept() 
    File "C:\Python27\lib\socket.py", line 206, in accept 
    sock, addr = self._sock.accept() 
    File "C:\Python27\lib\socket.py", line 174, in _dummy 
    raise error(EBADF, 'Bad file descriptor') 
socket.error: [Errno 9] Bad file descriptor 
+0

任何人有任何想法,为什么我会得到这个错误

尝试修复缩进? – user2593590

回答

0

我想即使连接也不可用的套接字发生错误广告还活着。在

S.CLOSE()

. 
. 
while 1: 
    conn, addr = s.accept() 
    print "Connected with the : " + addr [0] + ' : ' +str(addr[1]) 

s.close()