First commit
This commit is contained in:
commit
9fc4620088
47
pychat0.py
Normal file
47
pychat0.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
from socket import *
|
||||||
|
from _thread import start_new_thread
|
||||||
|
|
||||||
|
VERSION = "PYCHAT_0.0"
|
||||||
|
PORT = 6783
|
||||||
|
|
||||||
|
s = socket(AF_INET, SOCK_DGRAM)
|
||||||
|
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
||||||
|
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
||||||
|
s.bind(('', PORT))
|
||||||
|
|
||||||
|
def recieveThread():
|
||||||
|
while True:
|
||||||
|
b, a = s.recvfrom(4096)
|
||||||
|
if len(b) < 1:
|
||||||
|
print(a)
|
||||||
|
break
|
||||||
|
print(b.decode("utf-8"))
|
||||||
|
|
||||||
|
|
||||||
|
name = ''
|
||||||
|
while name == '':
|
||||||
|
print("Enter username:", end="")
|
||||||
|
name = input()
|
||||||
|
if(len(name) > 20):
|
||||||
|
print("Error: username must be less than 20 characters")
|
||||||
|
name = None
|
||||||
|
name = name.lstrip()
|
||||||
|
name = name.rjust(19)
|
||||||
|
|
||||||
|
def sendThread():
|
||||||
|
while True:
|
||||||
|
#print(name + ' > ', end='')
|
||||||
|
msg = name + ' | ' + input()
|
||||||
|
s.sendto(msg.encode('utf-8'), ('255.255.255.255', PORT))
|
||||||
|
|
||||||
|
start_new_thread(recieveThread, ())
|
||||||
|
#start_new_thread(sendThread, ())
|
||||||
|
try:
|
||||||
|
sendThread()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Closing socket")
|
||||||
|
s.close()
|
||||||
|
except EOFError:
|
||||||
|
print("Closing socket")
|
||||||
|
s.close()
|
||||||
|
|
||||||
23
server.py
Normal file
23
server.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import socket # Import socket module
|
||||||
|
import sys
|
||||||
|
|
||||||
|
s = socket.socket() # Create a socket object
|
||||||
|
host = socket.gethostname() # Get local machine name
|
||||||
|
port = 12345 # Reserve a port for your service.
|
||||||
|
try:
|
||||||
|
s.bind((host, port)) # Bind to the port
|
||||||
|
|
||||||
|
s.listen(5) # Now wait for client connection.
|
||||||
|
while True:
|
||||||
|
c, addr = s.accept() # Establish connection with client.
|
||||||
|
print('Got connection from', addr)
|
||||||
|
c.send(b'Thank you for connecting\n')
|
||||||
|
c.close() # Close the connection
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
s.close()
|
||||||
|
print("Exiting Server")
|
||||||
|
sys.exit()
|
||||||
|
finally:
|
||||||
|
s.close()
|
||||||
22
server2.py
Normal file
22
server2.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import os
|
||||||
|
import socket
|
||||||
|
from multiprocessing import Process
|
||||||
|
|
||||||
|
def handle_client(cli: socket, addr):
|
||||||
|
# For now just print out the received bytes and close
|
||||||
|
print("Received connection from", addr)
|
||||||
|
recv_data = cli.recv(4096)
|
||||||
|
print(recv_data.decode("utf-8"))
|
||||||
|
cli.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
address = ''
|
||||||
|
port = 8080
|
||||||
|
so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
so.bind((address, port))
|
||||||
|
so.listen(5)
|
||||||
|
while True:
|
||||||
|
(client_socket, client_address) = so.accept()
|
||||||
|
print("Client connected")
|
||||||
|
Process(target=handle_client, args=(client_socket, client_address)).start()
|
||||||
24
teapotd.py
Normal file
24
teapotd.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import socket # Import socket module
|
||||||
|
import sys
|
||||||
|
|
||||||
|
s = socket.socket() # Create a socket object
|
||||||
|
host = socket.gethostname() # Get local machine name
|
||||||
|
port = 8080 # Reserve a port for your service.
|
||||||
|
try:
|
||||||
|
s.bind((host, port)) # Bind to the port
|
||||||
|
|
||||||
|
s.listen(5) # Now wait for client connection.
|
||||||
|
while True:
|
||||||
|
c, addr = s.accept() # Establish connection with client.
|
||||||
|
print('Got connection from', addr)
|
||||||
|
print(c.recv(1024))
|
||||||
|
c.send(b"HTTP/1.0 418 I'm a Teapot\r\nConnection: close\r\nServer: Teapot\r\n\r\nError 418 I'm a teapot")
|
||||||
|
c.close() # Close the connection
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
s.close()
|
||||||
|
print("Exiting Server")
|
||||||
|
sys.exit()
|
||||||
|
finally:
|
||||||
|
s.close()
|
||||||
Loading…
x
Reference in New Issue
Block a user