Jef*_*rth 2 python sockets network-programming
我应该使用什么库进行网络编程?是sockets最好的,还是有更高级别的界面,这是标准的?
我需要一些非常跨平台的东西(即Linux,Windows,Mac OS X),它只需要能够使用相同的库连接到其他Python程序.
你只想在节点之间发送python数据(可能在不同的计算机上)?您可能想要查看SimpleXMLRPCServer.它基于内置的HTTP服务器,它基于内置的Socket服务器,它们都不是最具工业强度的服务器,但它很容易设置匆忙:
from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(("localhost", 9876))
def my_func(a,b):
return a + b
server.register_function(my_func)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
并且易于连接到:
import xmlrpclib
s = xmlrpclib.ServerProxy('http://localhost:9876')
print s.my_func(2,3)
>>> 5
print type(s.my_func(2,3))
>>> <type 'int'>
print s.my_func(2,3.0):
>>> 7.0
Run Code Online (Sandbox Code Playgroud)
Twisted在工业应用中很受欢迎,但它有一个残酷的学习曲线.
答案取决于你想要做什么.
"我应该使用什么库进行网络编程?" 很模糊.
例如,如果你想做HTTP,你可能会看到像urllib,urllib2,httplib,套接字这样的标准库.这一切都取决于您要使用的协议,以及您希望使用的网络层.
python中有各种网络任务的库...电子邮件,网络,rpc等等......
对于初学者,查看标准库参考手册并查看您要执行的任务,然后从那里开始:http://docs.python.org/library/index.html
| 归档时间: |
|
| 查看次数: |
1085 次 |
| 最近记录: |