点击(此处)折叠或打开
- import socket
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- port = input("port: ")
- sock.bind(('localhost', port))
- print "serving on port %s" %(port)
- sock.listen(1)
- while 1:
- connection,address = sock.accept()
- while 1:
- print "receiving ..."
- buf = connection.recv(1024)
- if buf == '1': connection.send('welcome to server!')
- elif buf == 'shutdown': connection.close();break
- else: connection.send(buf);print buf
点击(此处)折叠或打开
- import socket
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- port = input("port: ")
- sock.connect(('localhost', port))
- while 1:
- data = raw_input("data to send: ")
- if data == "quit":
- sock.close()
- break
- sock.send(data)
- print "receiving..."
- datas = sock.recv(1024)
- print datas