Send and Receive data between .py files

3

I'm starting to study Python and I have the following doubt, maybe because I'm used to php and with Python it's done in a different way, I want to send and receive data from one .PY file to another.

file SEND.PY

import requests
datos = {"nombre":"Nodgard"}
respuesta = requests.post("recibe.py",data=datos)
print respuesta.text

RECIBE.PY file

import cgi
form = cgi.FieldStorage()
print form

In the file Send.py it gives me the following error: Invalid URL 'recib.py', is because requests only allows to interact with web addresses? How could I do it with .py files that are locally?

    
asked by StevePHP 15.06.2017 в 16:25
source

1 answer

1

after reading and rereading I think what you need are sockets

SEND

# ENVIAR.py" 
# -*- coding: latin-1 -*
import socket              
# Creamos un objeto de clase socket 
s = socket.socket()
# Indicamos el host y el puerto que estar a la escucha con una tupla 
s.bind(("localhost", 9999))
# Metodo listen para aceptar conexiones entrantes # con el numero maximo de conexiones aceptadas 
s.listen(1) 
# Metodo accept para escuchar # Bloquea la ejecucion hasta que llega un mensaje # Cuando este llega, devuelve un objeto socket # y una tupla con el host y el puerto de la conexion 
c, addr_socketc = s.accept() 

print("Conexion desde", addr_socketc)

# Esperamos un mensaje del cliente 
recibido = c.recv(1024) 
print(recibido.decode("utf-8"))
# Cerramos los sockets
c.close()
s.close()

RECEIVE

# "RECIBE.py" 
# -*- coding: latin-1 -*
import socket
# Creamos un objeto clase socket 
s = socket.socket()
# Intentamos conectar con un puerto de un host 
s.connect(("localhost", 9999))
# Enviamos un mensaje al servidor 

s.send(bytes("mensaje del cliente", "utf-8"))

# Cerramos el socket s.close()

OBVIOUSLY there is a lot of fabric to cut talking about sockets because for a good operation they must join with threads and other things ...

a good example:

link

    
answered by 22.06.2017 в 02:59