因工作需求,接触了一下python,感觉跟c差不多,尝试写了个小程序。
客户端:
- #!/usr/bin/python
- #encoding = utf-8
- import socket
- s = socket.socket()
- server = socket.gethostname()
- print server
- port = 8888
- s.connect((server,port))
- print s.recv(1024)
- while True:
- say = raw_input(u'client:\n '.encode('gbk')) //使用raw_input()函数取得用户输入的数据
- s.send(str(say))
- print u'server'
- print ' %s'%s.recv(1024)
- s.close()
- raw_input()
服务器:
- #!/usr/bin/python
- #encoding = utf-8
- import socket
- s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- host = ''
- port = 8888
- s.bind((host,port))
- s.listen(10)
- c,addr = s.accept()
- print 'get connection from',addr
- c.send('this is a simple server')
- while True:
- print u'client: '
- print ' %s'%c.recv(1024)
- ssay = raw_input(u'server:\n '.encode('gbk'))
- c.send(str(ssay))
- c.close()
- raw_input()
阅读(1516) | 评论(0) | 转发(0) |