In this line -> tcpSerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
what is the first 'socket' after the '=' sign, is that refering to a socket class making tcpSerSock an object or just to a module called socket?
After reading the documentation I see that socket.accept returns a connection and an address pair, but it doesn't say what is returns them in, a list or a tuple or something else? So how would this line work
-> tcpCliSock, addr = tcpSerSock.accept()
?
And can anyone tell me why the following two server/client programs are not working on my system (Linux)? When I run the client it says -> tcpCliSock.connect(ADDR) -> OSError: [Errno 101] Network is unreachable
Client
#!/usr/bin/python3
from socket import *
HOST = '127.0.0.1' # or 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
while(True):
data = input('> ')
if not data:
break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print(data.decode('utf-8'))
tcpCliSock.close()
Server
#!/usr/bin/python3
from socket import *
from time import ctime
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print('waiting for connection...')
tcpCliSock, addr = tcpSerSock.accept()
print('...connected from:', addr)
while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpCliSock.send('[%s] %s' % (bytes(ctime(), 'utf-8'), data))
tcpCliSock.close()
tcpSerSock.clsoe()