| [ Web Proxy ] |
| Viewing: https://pythonprogramming.net/client-server-python-sockets/ | [Back] [Original] |
Now, let's create a simple server system that accepts incoming connections and some data we type. A small warning: this is highly unlikely to work well on Windows operating systems. You will still get some output, but it will likely be for every key pressed.
import socket
import sys
from _thread import *
host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
except socket.error as e:
print(str(e))
s.listen(5)
print('Waiting for a connection.')
def threaded_client(conn):
conn.send(str.encode('Welcome, type your info\n'))
while True:
data = conn.recv(2048)
reply = 'Server output: '+ data.decode('utf-8')
if not data:
break
conn.sendall(str.encode(reply))
conn.close()
while True:
conn, addr = s.accept()
print('connected to: '+addr[0]+':'+str(addr[1]))
start_new_thread(threaded_client,(conn,))
The next tutorial: Python 2to3 for Converting Python 2 scripts to Python 3
Contact: Harrison@pythonprogramming.net.
Programming is a superpower.
| Web Proxy Viewer | New URL | Original Page |