Python本地主机服务器/客户端网络代码来加/减/乘/除两个数字?
我最近一直在阅读《Python 网络编程基础》,因为我对计算机网络感兴趣,并且为了练习,我根据我在书中看到的前几个示例程序编写了一些代码。在这种情况下,我“编写”了一个绑定到本地主机和随机端口的 TCP 服务器以及连接到本地主机的客户端。客户端向服务器提供一个由 2 个数字和一个由空格分隔的操作组成的字符串(即“5 x 4”),服务器对此进行评估并返回适当的值。我的代码如下:
#!/usr/bin/env python
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '127.0.0.1'
PORT = 1060
if sys.argv[1] == 'server':
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
while True:
print 'Now listening at: ', s.getsockname()
sc, sockname = s.accept()
print 'We have accepted a connection from', sockname
print sc.getsockname(), 'is now connected to', sc.getpeername()
message = sc.recv(1024)
print 'The client wants to perform the operation: ' + message
message = message.split()
if message[1] == '+':
result = float(message[0]) + float(message[2])
elif message[1] == '-':
result = float(message[0]) - float(message[2])
elif message[1] == '*':
result = round(float(message[0]) * float(message[2]), 3)
elif message [1] == '/':
result = round(float(message[0]) / float(message[2]), 3)
sc.sendall('The result is ' + str(result))
sc.close()
print 'Reply sent as ' + str(result) + '.'
print
elif len(sys.argv) == 5 and sys.argv[1] == 'client':
s.connect((HOST, PORT))
print 'You are now connected to: ', s.getsockname()
s.sendall(sys.argv[2] + ' ' + sys.argv[3] + ' ' + sys.argv[4])
reply = s.recv(1024)
print 'The return value is', repr(reply)
s.close()
else:
print >>sys.stderr, 'usage: addStream.py server or addStream.py client num1 +/-/*// num2'
我的问题是:这是最好的方法来做到这一点或者有更好的方法吗?
谢谢!
I've recently been reading "Foundations of Python Network Programming" as I am interested in computer networks and for practice made up some code based off of the first few sample programs I saw in the book.. in this case I "wrote" a TCP server that binds to the localhost and a random port and a client that connects to the localhost. The client gives the server a string consisting of 2 numbers and an operation separated by spaces (i.e. '5 x 4') and the server evaluates this and returns the appropriate value.. my code is as follows:
#!/usr/bin/env python
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '127.0.0.1'
PORT = 1060
if sys.argv[1] == 'server':
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
while True:
print 'Now listening at: ', s.getsockname()
sc, sockname = s.accept()
print 'We have accepted a connection from', sockname
print sc.getsockname(), 'is now connected to', sc.getpeername()
message = sc.recv(1024)
print 'The client wants to perform the operation: ' + message
message = message.split()
if message[1] == '+':
result = float(message[0]) + float(message[2])
elif message[1] == '-':
result = float(message[0]) - float(message[2])
elif message[1] == '*':
result = round(float(message[0]) * float(message[2]), 3)
elif message [1] == '/':
result = round(float(message[0]) / float(message[2]), 3)
sc.sendall('The result is ' + str(result))
sc.close()
print 'Reply sent as ' + str(result) + '.'
print
elif len(sys.argv) == 5 and sys.argv[1] == 'client':
s.connect((HOST, PORT))
print 'You are now connected to: ', s.getsockname()
s.sendall(sys.argv[2] + ' ' + sys.argv[3] + ' ' + sys.argv[4])
reply = s.recv(1024)
print 'The return value is', repr(reply)
s.close()
else:
print >>sys.stderr, 'usage: addStream.py server or addStream.py client num1 +/-/*// num2'
My question is: is this the best way to do this or is there a better way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Twisted 是世界上最好的网络引擎。它将在一段漂亮且友好的代码块中处理您想要的所有内容,这对您的机器来说对 CPU 和 IO 都很友好。
http://twistedmatrix.com/trac/
Twisted is about the best network engine on offer anywhere. It will handle everything you want in a nice and friendly chunk of code which is both CPU and IO friendly to your machine.
http://twistedmatrix.com/trac/
我用它来做一些小事情: http://docs.python.org/library/ asynchat.html
正如文档所说,它比 cpu 绑定服务器更适合 i/o 绑定服务器。
I've used this for a few little things: http://docs.python.org/library/asynchat.html
As the docs say, it's more appropriate for i/o bound servers than cpu bound servers.
展望未来,使用某种网络包将变得更加容易,而不仅仅是依赖 python 标准库。我认为 0MQ 将是一个很好的起点,它易于使用和学习 python 绑定,您正在学习的大部分内容仍然适用,而且它快速高效。
最重要的是,它不会将应用程序的所有部分与 python 语言联系起来,并且它不关心传输方法、进程内、进程间、tcp...
http://zeromq.github.com/pyzmq/
Going forward it is going to be much easier to use some sort of network package instead of just relying on the python standard library. I think 0MQ would be a good place to start, it has easy to work with and learn python bindings, most of what you are learning already still applies, and it is fast and efficient.
On top of that it doesn't tie you to the python language for all parts of the application, and it doesn't care about the transport method, intraprocess, interprocess, tcp...
http://zeromq.github.com/pyzmq/