Skip to content

Commit

Permalink
UDP add
Browse files Browse the repository at this point in the history
  • Loading branch information
May199 committed Apr 17, 2021
1 parent d540e4c commit 206b60e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions UDP/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/__pycache__
/.vscode
19 changes: 19 additions & 0 deletions UDP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1 align="center"> :construction: Python Server :snake: Em construção... :construction: </h1>

### Concluídos

- [x] Criar um server Python
- [x] Fazer o server retornar uma página Web
- [x] Criar requisições GET
- [ ] Criar requisições POST
- [ ] Criar conexões entre cliente/servidor

### Como rodar o projeto

$ cmd.exe
$ cd C:\Users\nomeUsuário\diretorio_do_projeto\python-server
$ py main.py

### Tecnologias
- [Python](https://www.python.org/)
- [Bootstrap](https://getbootstrap.com/)
27 changes: 27 additions & 0 deletions UDP/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import socket

class Client():

def __init__(self, address = 'localhost', port = 3000):
self.address = address
self.port = port

def connect(self):
server = (self.address, self.port)

conection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
conection.bind((self.address, self.port))

msg = input('-> ')

while msg != 'closed':
conection.sendto(msg.encode(), server)
date, addressConection = conection.recvfrom(1024)

print('received -> ', str(date))

msg = input('-> ')

if __name__ == '__main__':
cn = Client()
cn.connect()
5 changes: 5 additions & 0 deletions UDP/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from server import Server

if __name__ == '__main__':
sr = Server()
sr.connect()
31 changes: 31 additions & 0 deletions UDP/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# UDP
# Protocolo que não se basea em uma conexão em que se manda dados sem segurança com relação ao seu ddestino.
import socket

class Server:

def __init__(self, address = '', port = 3000):
self.address = address
self.port = port

def connect(self):
#Criando socket com o protocolo UDP utilizando 'SOCK_DGRAM'

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as server:
# Bind() só recebe 1 paramentro então utilizando as (()) conseguimos burlar isso já que teoricamente é apenas 1
server.bind((self.address, self.port))

while True:
print('Connecting to server')
date, addressConection = server.recvfrom(1024)


print('message received from: ', str(addressConection))
print('received customer: ', str(date))

response = str(date)
server.sendto(date, addressConection)




Binary file removed __pycache__/webserver.cpython-37.pyc
Binary file not shown.
Empty file removed index.html
Empty file.

0 comments on commit 206b60e

Please sign in to comment.