some python sources
#!/usr/bin/python3
#this is tcp server.py script
#aron
import socket
#create
s=socket.socket()
#s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#ip + port
host = socket.gethostname
port=9999
ip='0.0.0.0'
#listen addr
host=''
s.bind((ip,port))
print("Waiting for connection....")
s.listen(5)
#create with client
while True:
conn,addr=s.accept()
print('Got connection for form',addr)
conn.send('server saying hi'.encode())
conn.close()
#close
#!/usr/bin/python3
#this is tcp client.py script
#aron
import socket
#create
s=socket.socket()
#s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#ip + port
host = socket.gethostname
port=9999
ip='127.0.0.1'
#listen addr
host=''
s.connect((ip,port))
print(s.recv(1024).decode('utf-8'))
s.close()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("127.0.0.1", 6000))
print("UDP bound on port 6000...")
while True:
data, addr = s.recvfrom(1024)
print("Receive from %s:%s" % addr)
if data == b"exit":
s.sendto(b"Good bye!", addr)
continue
s.sendto(b"Hello %s!" % data, addr)
UDP bound on port 6000...
Receive from 127.0.0.1:41088
Receive from 127.0.0.1:41088
Receive from 127.0.0.1:41088
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
/tmp/ipykernel_43/1552859108.py in <module>
8
9 while True:
---> 10 data, addr = s.recvfrom(1024)
11 print("Receive from %s:%s" % addr)
12 if data == b"exit":
KeyboardInterrupt:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = ("127.0.0.1", 6000)
while True:
data = input("Please input your name: ")
if not data:
continue
s.sendto(data.encode(), addr)
response, addr = s.recvfrom(1024)
print(response.decode())
if data == "exit":
print("Session is over from the server %s:%s" % addr)
break
s.close()
#!/usr/bin/python3
#this is tcp echo-server.py script
#aron
import socket
ip='127.0.01'
port=54321
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.bind((ip,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)
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
/tmp/ipykernel_17/1332507497.py in <module>
9 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
10 s.bind((ip,port))
---> 11 s.listen()
12 conn,addr=s.accept()
13 with conn:
OSError: [Errno 95] Operation not supported