Advertisement
zhenialeks

sockets

May 29th, 2020
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #server
  4.  
  5. import socket
  6. import urllib.request
  7.  
  8. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9.  
  10. sock.bind(('localhost', 9000))
  11. sock.listen(1)
  12.  
  13. conn,addr = sock.accept()
  14.  
  15. url = "http://linux.org"
  16. request = urllib.request.Request(url)
  17. response = urllib.request.urlopen(request)
  18. data = response.read().decoding("utf-8")
  19.  
  20. conn.send(data)
  21.  
  22. conn.close()
  23.  
  24. ##################################################################3
  25.  
  26.  
  27. #!/usr/bin/env python
  28. # -*- coding: utf-8 -*-
  29. #client
  30. import socket
  31.  
  32. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  33.  
  34. sock.connect(('localhost', 9000))
  35.  
  36.  
  37. data = None
  38. chunk = sock.recv(1024)
  39. while chunk:
  40.     data += chunk
  41.     chunk = sock.recv(1024)
  42.  
  43. sock.close()
  44.  
  45. with open("output.txt", "w") as f:
  46.     f.write(data.decode("utf-8"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement