Skip to content

Commit d8226bd

Browse files
committed
Add socket
1 parent 195f7fa commit d8226bd

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ The [file operations](/file-operations) encrypts a text using the caesar cipher(
2424

2525
The [object serializer](/object-serializer) takes an object as a argument and serializes into `json`.
2626

27+
### Socket
28+
29+
[client]: /socket/client.py
30+
[server]: /socket/server.py
31+
32+
[socket](/socket) has a [`client`][client] and a [`server`][server]. The [`client`][client] sends to the [`server`][server] a "`Hello, World`", which in return sends back to the [`client`][client] the same message.
33+
2734
## Learning source
2835

2936
* [A byte of python(PDF)](https://edisciplinas.usp.br/pluginfile.php/3252353/mod_resource/content/1/b_Swaroop_Byte_of_python.pdf)
@@ -42,6 +49,8 @@ The [object serializer](/object-serializer) takes an object as a argument and se
4249

4350
* [Code Nation Challenge](https://github.com/rjLelis/code-nation-challenge)
4451

52+
* [Aceleração python codenation](https://github.com/rjLelis/aceleracao-python)
53+
4554
## Some of my Django projects
4655

4756
* [blog_django](https://github.com/rjLelis/blog_django)

socket/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import socket
2+
3+
HOST = '127.0.0.1'
4+
PORT = 8000
5+
6+
if __name__ == "__main__":
7+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
8+
s.connect((HOST, PORT))
9+
s.sendall(b'Hello, World')
10+
data = s.recv(1024)
11+
12+
print('Recebido', data)

socket/server.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import socket
2+
3+
HOST = '127.0.0.1'
4+
PORT = 8000
5+
6+
if __name__ == "__main__":
7+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
8+
s.bind((HOST, PORT))
9+
s.listen()
10+
print(f'Esperando conexão em {HOST}:{PORT}')
11+
conn, addr = s.accept()
12+
with conn:
13+
print('Conectado por', addr)
14+
while True:
15+
data = conn.recv(1024)
16+
if not data:
17+
break
18+
conn.sendall(b'Server ' + data)

0 commit comments

Comments
 (0)