Gå til innhold

[Python/Generelt] Hvordan finne adresse til pc


Anbefalte innlegg

Litt dårlig tittel, men det jeg er ute etter er å finne ip adressen til en pc. Jobber med et/to programmer som skal kunne sende og motta filer over nettet og bruker sockets til dette, men jeg må jo ha en adresse å koble til så jeg lurer på hvordan jeg kan finne denne.

 

Koden jeg bruker er gjenngitt under(bare å ignorere kommentarene)

 

 

Programmet som sender:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import socket

class Sender():

   def send(self, host, size, filename):
       #This is used to send a file to the 'host'
       server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       server.connect((host, 59333))
       info = server.sendall('{0},{1}'.format(size, filename))

       f = open(filename, 'rb')
       while info == None:
           data = f.read(4096)
           if not data:
               break
           try:
               server.sendall(data)
           except socket.error, e:
               print 'An error occured, the error was\n%s'%e
       f.close()
       server.close()

   def getSize(self, filename):
       #This returns the size/4096 and is used to insure
       #that the whole file is beeing sent
       f = open(filename, 'rb')
       size = 0
       while True:
           data = f.read(4096)
           if not data:
               f.close()
               break
           size += 1
       return size

if __name__ == '__main__':
   import sys
   sender = Sender()
   sender.send('localhost', sender.getSize(sys.argv[1]), sys.argv[1])

Programmet som mottar:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import threading
import socket

class Reciver(threading.Thread):

   def __init__(self):
       self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       self.server.bind(('', 59333))
       self.server.listen(1)
       print 'Your host address {0}:{1}'.format(socket.gethostbyname(socket.gethostname()), 59333)
       threading.Thread.__init__ ( self )

   def run(self):
       while True:
           channel, details = self.server.accept()
           print 'Received connection:', details[0]
           #Here we first accept a small message conntaining
           #size of the file in bytes and filename
           #It should come as a string: 'filesize,filename'
           #filesize should be number of times 4096 goes up in the file
           size, filename = (channel.recv(1024)).split(',')
           #print size, filename
           #Gui should ask user for permission here
           if True:
               #If given permission Gui should ask where to store file
               f = open(filename, 'wb')
               writen = 0
               while True:
                   #Here the Gui can monitor how much we
                   #have downloaded, by comparing it with the size
                   #we got earlier
                   data = channel.recv(4096)
                   if not data:
                       break
                   writen += 1
                   f.write(data)
               if writen != int(size):
                   #There should also be an error here which the Gui can pick up
                   print 'An error occured, did not recive full size\nRecived %s, but should have gotten %s' %(writen, size)
               else:
                   print 'File recived complete!'
               f.close()
               channel.close()
               print 'Closed connection:', details[0]
               import sys
               sys.exit(1)

   def getHost(self):
       return socket.gethostbyname(socket.gethostname())

   def close(self):
       self.server.close()

if __name__ == '__main__':
   Reciver().start()

 

 

Lenke til kommentar
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...