python的三个函数(eval、exec、complie)和python版RMI
生活随笔
收集整理的這篇文章主要介紹了
python的三个函数(eval、exec、complie)和python版RMI
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、python的三個函數:
1、eval函數:
之前已經講過了這個函數,該函數也類似于php的eval,例如下邊這個例子
1 eval("os.system('id')")
但是有個問題,在eval中沒辦法執行賦值操作。
2、exec函數:
可以執行賦值操作,其他方便也比較類似于eval函數
1 exec("os.system('id')")
給一個賦值操作的例子:
1 exec("a.__code__=b.__code__")
3、compile函數:
其實這個函數有點類似醫院掛號分診的功能:
舉兩個例子說明吧:
1 exec_code = compile("""for i in range(4):print i""","","exec")
2 exec(exec_code)
3 eval_code = compile("os.system('id')","","eval")
4 eval(eval_code)
二、python的RMI
1、注冊對象:
(1)服務端腳本
1 #服務器端:
2 import SimpleXMLRPCServer
3
4 class MyObject:
5 def sayHello(self):
6 return "hello xmlprc"
7
8 obj = MyObject()
9 server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8088))
10 server.register_instance(obj)
11
12 print "Listening on port 8088"
13 server.serve_forever()
(2)客戶端腳本
1 import xmlrpclib
2
3 server = xmlrpclib.ServerProxy("http://localhost:8088")
4
5 words = server.sayHello()
6
7 print "result:" + words
(3)備注:我發現的問題——調用對象的沒有return的函數會導致崩潰打印堆棧,不知道為啥。
2、注冊函數:
(1)服務端腳本
1 from SimpleXMLRPCServer import SimpleXMLRPCServer
2 def respon_string(str):
3 return "get string :%s"%str
4
5 if __name__ == '__main__':
6 s = SimpleXMLRPCServer(('0.0.0.0', 8080))
7 s.register_function(respon_string,"get_string")
8 s.serve_forever()
(2)客戶端腳本:
1 from xmlrpclib import ServerProxy
2 if __name__ == '__main__':
3 s = ServerProxy("http://192.168.137.9:8080")
4 print s.get_string("hello")
(3)備注:我發現的問題——調用對象的沒有return的函數會導致崩潰打印堆棧,不知道為啥。
總結
以上是生活随笔為你收集整理的python的三个函数(eval、exec、complie)和python版RMI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决布局拖动混乱的问题
- 下一篇: 一本通1156 求π的值