Está en la página 1de 23

Programando Sockets / Threads

Java/Python

Javier Alexander Hurtado


javhur@unicauca.edu.co
@javhur
¿Comunicación?

¿Cómo transferir datos de un lugar a


otro? ¿De una máquina a otra?
Protocolos
Sockets

Socket
0 → 65.535 Socket

Puerto Puerto

Dirección Dirección
IP TCP / UDP IP
El ejemplo

?
Networking

JAVA

6
Socket TCP servidor

Socket cliente;

ServerSocket server = new


ServerSocket(5000);

cliente = server.accept();
Socket TCP cliente

int port = 5000;


String ip = “192.168.121.10”;
Socket socket = new Socket(ip, port);
socket.close();

Socket cliente
¿Y los datos?

BufferedReader buf = new


BufferedReader(new
InputStreamReader(socket.getInputStre
am()));

DataOutputStream out = new


DataOutputStream(socket.getOutputStre
am());
Java Threads

Subproceso
2

Subproceso
1
Proceso
principal

10
¿Cómo construirlo?

public class Hilo extends Thread {


public void run() {}
}

public class Hilo2 implements


Runnable {
public void run() {}
}
Networking

PYTHON

12
El lenguaje Python

Python

2.X 3.X
(Oct. 16 / 2000) (Dec. 3 / 2008)

2.7.11 (2010) 3.5.1 (Feb, 2015)

Modules
Twisted, PyQT, PySide, numpy, Django, Flask, py2exe, etc.
¿Cual version usar?

• 2.7? (2010)
– No more releases.
– Extended support for this end-of-life release
• 3.5? (2015)
– Standard library improvements, for example, are only
available by default in Python 3.x.
– 5 years of background
¿Cómo funciona?

• Python
• Anaconda (Continuum)
– IDE: Spyder (Data Scientist)
– Management by Command line
Algunos comandos

• >conda

open source package management system and environment


management system
Algunos comandos

• >python --version
• >conda
• >conda --version
• >conda update --help
• >conda update conda
• >conda update anaconda
• >conda update python
• >conda search python
Algunos comandos

• >conda create --name py35 python=3


anaconda
• >conda info –envs
• >conda remove –name py35 –all
• >activate py35
Sockets python

socket(arg1, arg2)

Argumento 1 Argumento 2

socket.AF_UNIX socket.SOCK_STREAM
socket.AF_INET socket.SOCK_DGRAM
socket.AF_INET6 socket.SOCK_RAW
socket.SOCK_RDM
socket.SOCK_SEQPACKET
Server TCP en Python

#create an INET, STREAMING socket


serversocket =
socket.socket(socket.AF_INET,socket.SOCK
_STREAM)

#bind the socket to a public host


serversocket.bind((socket.gethostname(),
80))
#become a server socket
serversocket.listen(5)
Socket server Python
Server TCP en Python

while 1:
#accept connections from outside
(clientsocket, address) =
serversocket.accept()
#now do something with the
clientsocket
#in this case, we'll pretend this is
a threaded server
ct = client_thread(clientsocket)
ct.run()
Socket server Python
Socket TCP cliente en Python

#create an INET, STREAMing socket


s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)

#connect to web server on port 80


s.connect(("www.website.com", 80))

Socket cliente Python


¡Gracias!
¿Preguntas?
Javier Alexander Hurtado
javhur@unicauca.edu.co
@javhur

También podría gustarte