#!/usr/bin/python # This is server.py file import os import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. input = os.listdir(".") # put list of dir into input strInput = ''.join(input) # Change into string so it can be sent c, addr = s.accept() # Establish connection with client. print 'Connection has been established from', addr c.send('.') while True: answer = c.recv(1024) if answer == "show dir": answer = strInput c.send(answer) else: c.send("Command not implemented") c.close # Close the connection #### #!/usr/bin/python # This is client.py file import os import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) # Connect print s.recv(1024) while 1: answer = raw_input('Please enter a command or q to quit: ') # wait for command from the keyboard if answer == 'show dir': s.send(answer) elif answer == "q": break else: print('Command not implemented') s.close # Close the socket when done
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question