python3 socket server code example
Example 1: client server python socket
import socket
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
Example 2: python3 socket server
import socket
import os
sock_file = "/tmp/python_socket"
if os.path.exists(sock_file):
os.remove(sock_file)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(sock_file)
sock.listen()
print("Listening...")
while True:
conn, _addr = sock.accept()
datagram = conn.recv(1024)
request = datagram.decode('utf-8')
conn.send(str("Back at you:"+request).encode('utf-8'))