Python creates server

Server side code.: server.py
import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host=socket.gethostname()
sock.bind((host,8001))
sock.listen(5)
while True:
connection=sock.accept()
try:
connection.settimeout(5)
buf=connection.recv(1024)
if bug == '1':
connection.send('Welcome to server!')
else:
connection.send('please go out!')
except socket.timeout:
print 'time out'
connection.close()

The client code.: client.py
import socket
import time
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host=socket.gethostname()
sock.connect((host,8001))
time.sleep(2)
sock.send('1')
receive=sock.recv(1024)
print receive
sock.close()


The first implementation of server.py, in the implementation of client.py
The shell implementation of the results is as follows, in accordance with the normal words should print'Welcome to server! '.
>>> ================================ RESTART ================================
>>>

>>> ================================ RESTART ================================
>>>

Started by David at December 16, 2016 - 8:43 PM

if bug == '1'????

Posted by Lance at December 28, 2016 - 9:27 PM

In a thread reply, but gave little information.
Just to read socket.py, connection=sock.accept () returns a tuple (socket, addr)
So, the code should be: connection[0].settimeout(5)
Because tuple is not setTimeout method.

Posted by Marks at January 04, 2017 - 10:46 PM

if bug == '1': ? ? ? ? ? ?

Posted by Werner at January 06, 2017 - 10:13 PM

Bug there should be his wrong, put buf into bug.
But he simply can't run code before there is a problem, ha ha.

Posted by Marks at January 12, 2017 - 11:15 PM