title: Python - Sending files over sockets
body: <p>I have been trying to implement a small SERVER - CLIENT app but ran in to a errrr:</p>
  
  <blockquote>
    <p>socket.error: [Errno 10057] A request to send or receive data was
    disallowed because the socket is not connected and (when sending on a
    datagram socket using a sendto call) no address was supplied</p>
  </blockquote>
  
  <p>Server:</p>
  
  <pre><code>class Server:
      gate = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      host = socket.gethostname()
      port = 0
      file = ''
  
      def __init__(self, port):
          self.port = port
          self.gate.bind((self.host, self.port))  
          self.listen()
  
      def listen(self):
          self.gate.listen(10)
          while True:
              add = self.gate.accept()
              self.reciveFileName()
              self.reciveFile()
  
  
      def reciveFileName(self):
          while True:
              data = self.gate.recv(1024)
              self.file = data
  
      def reciveFile(self):
          createFile = open("new_"+self.file, "wb")
          while True:
              data = self.gate.recv(1024)
              createFile.write(data)
          createFile.close()
  
  
  
  a = Server(1111)
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>class Client:
      gateway = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      #host = socket.gethostname()
      host = ''
      port = 0
      file = ''
  
      def __init__(self, host, port, file):
          self.port = port
          self.host = host
          self.file = file
          self.connect()
  
      def connect(self):
          self.gateway.connect((self.host, self.port))
          self.sendFileName()
          self.sendFile()
  
      def sendFileName(self):
          self.gateway.send("name:" + self.file)
  
      def sendFile(self):
          readByte = open(self.file, "rb")
          data = readByte.read()
          readByte.close()
  
          self.gateway.send(data)
          self.gateway.close()
  
  
  
  a = Client('93.103.56.251', 1111, 'data.txt')
  </code></pre>