2016-08-14 4 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でlocalhostに接続しようとしています

を、私は、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()