toko214

server page 101

Feb 26th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. import socket
  2. import os
  3. x = 0
  4. s = socket.socket()
  5. host = socket.gethostname()
  6. port = 80
  7. s.bind(( host , port))
  8. s.listen(5)
  9. name = "file-name="
  10. iname = "image-name="
  11. while True:
  12.     c, addr = s.accept()     # Establish connection with client.
  13.     print 'Got connection from', addr
  14.     print "Receiving..."
  15.     l=c.recv(1024)
  16.     if ("favicon.ico" in l):
  17.                 continue
  18.     if ("GET /" in l) and (" HTTP/1.1" in l):
  19.          print (l)
  20.          index = l.index(iname)
  21.          index = index+len(iname)
  22.          index2 = l.index("HTTP")
  23.          file_name=l[index:index2]
  24.          print(file_name)
  25.          isf=os.path.isfile("upload/"+file_name)
  26.          if isf:
  27.              f=open("upload/"+file_name,'rb')
  28.              l=f.read()
  29.              while (l):
  30.                 c.send(l)
  31.                 l = f.read(1024)
  32.              print(file_name)
  33.              c.send(file_name)
  34.              c.close()
  35.              f.close()
  36.          else:
  37.             f=open("upload\e4.html",'rb')
  38.             l=f.read()
  39.             while (l):
  40.                 c.send(l)
  41.                 l = f.read(1024)
  42.             c.close()
  43.             f.close()
  44.     else:
  45.         index=l.index(name)
  46.         index=index+len(name)
  47.         index2=l.index("HTTP")
  48.         file_name=l[index:index2]
  49.         print(file_name)
  50.         f=open('upload/'+"("+str(x)+")"+file_name, 'wb')
  51.         while l:
  52.                l=c.recv(1024)
  53.                f.write(l)
  54.         f.close()
  55.         print "Done Receiving"
  56.         c.send('Thank you for connecting')
  57.         c.close()
  58.         x+=1
  59. print "server is shutting down"
  60. s.shutdown()
Add Comment
Please, Sign In to add comment