Mira todo el código que he echo para los dos (cliente y servidor) y abriendo los puertos, todavía no me deja me vota conexión refused.
mi idea, para no usar no-ip, es que cada vez que el servidor se encienda, envíe a una pagina el puerto, el nombre y la pagina obtiene la IP publica.
por otro lado, he abierto un puerto del router para la conexión.
el cliente lee también la pagina y obtiene la ip, nombre y puerto del servidor. y se trata de conectar a esa maquina.
pero aún me sigue la conexión refused:
código servidor:
# -*- coding: utf-8 -*-
import urllib2, urllib
import socket
import csv
import threading
import time
from socket import *
SIZE = 4
class CThread(threading.Thread):
def __init__(self,c):
threading.Thread.__init__(self)
self.conn = c
self.stopIt=False
def mrecv(self):
data = self.conn.recv(SIZE)
self.conn.send('OK')
msg = self.conn.recv(int(data))
return msg
def run(self):
while not self.stopIt:
msg = self.mrecv()
print 'recieved-> ',msg
def setConn(con1,con2):
dict={}
state = con1.recv(9)
con2.recv(9)
if state =='WILL RECV':
dict['send'] = con1 # server will send data to reciever
dict['recv'] = con2
else:
dict['recv'] = con1 # server will recieve data from sender
dict['send'] = con2
return dict
def msend(conn,msg):
if len(msg)<=999 and len(msg)>0:
conn.send(str(len(msg)))
if conn.recv(2) == 'OK':
conn.send(msg)
else:
conn.send(str(999))
if conn.recv(2) == 'OK':
conn.send(msg[:999])
msend(conn,msg[1000:]) # calling recursive
def main():
host = '0.0.0.0'
port = 9999
name = raw_input("nombre del servidor: ")
while True:
try:
url = "http://webparasubirelarchivo/index.php"
values = {'name':name,'puerto':port}
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
resp = urllib2.urlopen(req)
page = resp.read()
resp.close()
break
except:
print "error desconocido"
quit()
prender(host,name,port)
def prender(host,name,port):
soc = socket(AF_INET, SOCK_STREAM)
soc.bind((host,port))
soc.listen(5)
print "servidor",name,"encendido",host,port
(c1,a1) = soc.accept()
(c2,a2) = soc.accept()
dict = setConn(c1,c2)
thr = CThread(dict['recv'])
thr.start()
print "se conecto con: "+str(a1[0])+" por el puerto "+str(a1[1])
print "servidor",host,"encendido"
try:
while 1:
msend(dict['send'],raw_input())
except:
print 'closing'
thr.stopIt=True
msend(dict['send'],'bye!!!')# for stoping the thread
thr.conn.close()
soc.close()
main()
codigo del cliente:
import socket
import csv
import threading
import urllib2
SIZE =4
class client(threading.Thread):
def __init__(self,c):
threading.Thread.__init__(self)
self.conn = c
self.stopIt = False
def mrecv(self):
data = self.conn.recv(SIZE)
self.conn.send('OK')
return self.conn.recv(int(data))
def run(self):
while not self.stopIt:
msg = self.mrecv()
print 'recieved-> ',msg
def msend(conn,msg):
if len(msg)<=999 and len(msg)>0:
conn.send(str(len(msg)))
if conn.recv(2) == 'OK':
conn.send(msg)
else:
conn.send(str(999))
if conn.recv(2) == 'OK':
conn.send(msg[:999])
msend(conn,msg[1000:]) # calling recursive
def conet(name,port,host):
print "intentando conectar con: ",name,host,port
soc1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
soc1.connect((host,int(port)))
soc1.send('WILL SEND') # telling server we will send data from here
soc2 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
soc2.connect((host,int(port)))
soc2.send('WILL RECV') # telling server we will recieve data from here
print "te has conectado",name
thr = client(soc2)
thr.start()
try:
while 1:
msend(soc1,raw_input())
except:
print 'closing'
thr.stopIt=True
msend(soc1,'bye!!') # for stoping the thread
thr.conn.close()
soc1.close()
soc2.close()
def leer():
try:
archiv = open("tmp.csv","r")
arc_csv = csv.reader(archiv)
nume = 0
lista = []
for nombre,port,ip in arc_csv:
nume += 1
print str(nume)+" "+nombre
lista += [[nombre,port,ip]]
except:
print "no hay archivo"
return
conet(nombre,port,ip)
def url():
try:
url = urllib2.urlopen('http://webparasubirelarchivo/index.php')
texto_csv = csv.reader(url)
for nombre,puerto,ip in texto_csv:
archivo = open("tmp.csv","w")
archivo.write(nombre+","+puerto+","+ip)
archivo.close()
url.close()
except:
print " No hay conexcion con la pagina web, revisa tu conexion a internet"
return
leer()
url()