python serversocket tutorial code example
Example 1: socket programming in python
import socket
HOST = '127.0.0.1'
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
Example 2: python socket server
import socket
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind(('', 15555))
while True:
socket.listen(5)
client, address = socket.accept()
print "{} connected".format( address )
response = client.recv(255)
if response != "":
print response
print "Close"
client.close()
stock.close()