需要从OBJ-CIOS应用程序向Python脚本传递两个参数
我对编写一般代码很陌生,我需要一些帮助将Python脚本转换为iOS应用程序的OBJ-C 我有一个python脚本在网桥上运行XMLRPC服务器 我有一个用Xcode4编写的iOS应用程序,当按下按钮时需要调用网桥上运行的python脚本 我还有一个用Python编写的XMLRPC客户端脚本,可以访问XMLRPC服务器 基本上,我需要将Python XMLRPC客户端脚本转换为OBJ-C,以便在iOS应用程序上运行它 Python XMLRPC客户端脚本:需要从OBJ-CIOS应用程序向Python脚本传递两个参数,python,objective-c,ios,Python,Objective C,Ios,我对编写一般代码很陌生,我需要一些帮助将Python脚本转换为iOS应用程序的OBJ-C 我有一个python脚本在网桥上运行XMLRPC服务器 我有一个用Xcode4编写的iOS应用程序,当按下按钮时需要调用网桥上运行的python脚本 我还有一个用Python编写的XMLRPC客户端脚本,可以访问XMLRPC服务器 基本上,我需要将Python XMLRPC客户端脚本转换为OBJ-C,以便在iOS应用程序上运行它 Python XMLRPC客户端脚本: from xmlrpclib impo
from xmlrpclib import ServerProxy, Binary
import time
yourAddr = Binary('\x00\x00\x04') # SNAP Address of this client application
ourAddr = Binary('\x4c\x6f\x8c') # SNAP Address of this client application
CONN_TYPE_USB = 2
count =0
def listenForResponse(srv):
global count
""" Wait and listen for a mcast count message """
eventResp = srv.waitOnEvent(ourAddr,1)
if eventResp != None and eventResp['methodName'] == "setButtonCount":
# The count should be the first parameter
print "The button count = ", (eventResp['parameters'][0]), "\n"
else:
if eventResp == None:
print "time out "
else:
print "...not the response we were looking for; continue waiting "
print eventResp['methodName']
# print "mcastRpc setButtonCount to snap server"
# count = (count + 1) %100
# srv.mcastRpc( ourAddr, 1,5, 'setButtonCount',[count])
# time.sleep(1)
if __name__ == '__main__':
""" The main program loop for this example """
# Connect to SNAPconnect server via TCP port 8080
#
# srv = ServerProxy("http://192.168.0.x:8080", allow_none=False)
srv = ServerProxy("http://192.168.0.x:8080", allow_none=False) #call from the xmlrpc library
# srv = ServerProxy("public:public:http://192.168.1.106:8080")
# Connect to SNAP bridge node (We'll assume it is on the first USB port)
print "opening serial" #remote procedure call being executed on the E10, telling the E10 to listen to the SNAP bridge on Serial port #1
srv.connectSerial(1,"/dev/ttyS1") # for the e10
# Continually listen for the count
while True:
listenForResponse(srv)
15:48:15:611 INFO SNAPconnect Now listening for XML-RPC connections on port 8080
15:48:23:981 INFO http_server connectSerial(1, )
15:48:24:003 INFO http_server 192.168.0.x:51752 - - "POST / HTTP/1.1" 200 479
15:48:24:010 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.TypeError'>:connectSerial() takes at least 3 arguments (2 given)</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
15:54:00:505 INFO http_server connectSerial(1, /dev/ttyS1, )
15:54:00:534 INFO http_server 192.168.0.x:51772 - - "POST /RPC2 HTTP/1.0" 200 225
15:54:00:541 INFO http_server <?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>
15:54:00:573 INFO http_server waitOnEvent(4c6f8c, 1, )
NSString *const ourAddr = @"1";
NSString *const dev = @"/dev/ttyS1";
-(IBAction)getResponse:(id) sender{
NSArray *args = [NSArray arrayWithObjects: ourAddr, dev, nil];
NSString *server = @"http://192.168.0.x:8080";
XMLRPCRequest *waitOnEvent = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
[waitOnEvent setMethod:@"connectSerial" withObjects: args];
20:17:20:954 INFO http_server connectSerial(1, /dev/ttyS1, )
20:17:20:975 INFO http_server 192.168.0.x:52253 - - "POST / HTTP/1.1" 200 467
20:17:20:981 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.ValueError'>:Serial interface type must be an integer</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
我的Xcode代码:
-(IBAction)getResponse:(id) sender{
NSString *ourAddr = @"1";
NSString *dev = @"/dev/ttylS1";
NSString *server = @"192.168.0.x:8080";
XMLRPCRequest *waitOnEvent = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
[waitOnEvent setMethod:@"connectSerial" withObject:[NSString stringWithString: ourAddr]];
NSString *result=[self executeXMLRPCRequest:waitOnEvent];
[waitOnEvent release];
if( ![result isKindOfClass:[NSString class]] ) //err occured.
lblResponse = result; //@"error";
else
lblResponse.text = @"error";
}
- (id)executeXMLRPCRequest:(XMLRPCRequest *)req {
XMLRPCResponse *userInfoResponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:req];
return [userInfoResponse object];
}
按下应用程序中的“getResponse”按钮后,我从XMLRPC服务器获得的信息:
from xmlrpclib import ServerProxy, Binary
import time
yourAddr = Binary('\x00\x00\x04') # SNAP Address of this client application
ourAddr = Binary('\x4c\x6f\x8c') # SNAP Address of this client application
CONN_TYPE_USB = 2
count =0
def listenForResponse(srv):
global count
""" Wait and listen for a mcast count message """
eventResp = srv.waitOnEvent(ourAddr,1)
if eventResp != None and eventResp['methodName'] == "setButtonCount":
# The count should be the first parameter
print "The button count = ", (eventResp['parameters'][0]), "\n"
else:
if eventResp == None:
print "time out "
else:
print "...not the response we were looking for; continue waiting "
print eventResp['methodName']
# print "mcastRpc setButtonCount to snap server"
# count = (count + 1) %100
# srv.mcastRpc( ourAddr, 1,5, 'setButtonCount',[count])
# time.sleep(1)
if __name__ == '__main__':
""" The main program loop for this example """
# Connect to SNAPconnect server via TCP port 8080
#
# srv = ServerProxy("http://192.168.0.x:8080", allow_none=False)
srv = ServerProxy("http://192.168.0.x:8080", allow_none=False) #call from the xmlrpc library
# srv = ServerProxy("public:public:http://192.168.1.106:8080")
# Connect to SNAP bridge node (We'll assume it is on the first USB port)
print "opening serial" #remote procedure call being executed on the E10, telling the E10 to listen to the SNAP bridge on Serial port #1
srv.connectSerial(1,"/dev/ttyS1") # for the e10
# Continually listen for the count
while True:
listenForResponse(srv)
15:48:15:611 INFO SNAPconnect Now listening for XML-RPC connections on port 8080
15:48:23:981 INFO http_server connectSerial(1, )
15:48:24:003 INFO http_server 192.168.0.x:51752 - - "POST / HTTP/1.1" 200 479
15:48:24:010 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.TypeError'>:connectSerial() takes at least 3 arguments (2 given)</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
15:54:00:505 INFO http_server connectSerial(1, /dev/ttyS1, )
15:54:00:534 INFO http_server 192.168.0.x:51772 - - "POST /RPC2 HTTP/1.0" 200 225
15:54:00:541 INFO http_server <?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>
15:54:00:573 INFO http_server waitOnEvent(4c6f8c, 1, )
NSString *const ourAddr = @"1";
NSString *const dev = @"/dev/ttyS1";
-(IBAction)getResponse:(id) sender{
NSArray *args = [NSArray arrayWithObjects: ourAddr, dev, nil];
NSString *server = @"http://192.168.0.x:8080";
XMLRPCRequest *waitOnEvent = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
[waitOnEvent setMethod:@"connectSerial" withObjects: args];
20:17:20:954 INFO http_server connectSerial(1, /dev/ttyS1, )
20:17:20:975 INFO http_server 192.168.0.x:52253 - - "POST / HTTP/1.1" 200 467
20:17:20:981 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.ValueError'>:Serial interface type must be an integer</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
服务器给我这个:
from xmlrpclib import ServerProxy, Binary
import time
yourAddr = Binary('\x00\x00\x04') # SNAP Address of this client application
ourAddr = Binary('\x4c\x6f\x8c') # SNAP Address of this client application
CONN_TYPE_USB = 2
count =0
def listenForResponse(srv):
global count
""" Wait and listen for a mcast count message """
eventResp = srv.waitOnEvent(ourAddr,1)
if eventResp != None and eventResp['methodName'] == "setButtonCount":
# The count should be the first parameter
print "The button count = ", (eventResp['parameters'][0]), "\n"
else:
if eventResp == None:
print "time out "
else:
print "...not the response we were looking for; continue waiting "
print eventResp['methodName']
# print "mcastRpc setButtonCount to snap server"
# count = (count + 1) %100
# srv.mcastRpc( ourAddr, 1,5, 'setButtonCount',[count])
# time.sleep(1)
if __name__ == '__main__':
""" The main program loop for this example """
# Connect to SNAPconnect server via TCP port 8080
#
# srv = ServerProxy("http://192.168.0.x:8080", allow_none=False)
srv = ServerProxy("http://192.168.0.x:8080", allow_none=False) #call from the xmlrpc library
# srv = ServerProxy("public:public:http://192.168.1.106:8080")
# Connect to SNAP bridge node (We'll assume it is on the first USB port)
print "opening serial" #remote procedure call being executed on the E10, telling the E10 to listen to the SNAP bridge on Serial port #1
srv.connectSerial(1,"/dev/ttyS1") # for the e10
# Continually listen for the count
while True:
listenForResponse(srv)
15:48:15:611 INFO SNAPconnect Now listening for XML-RPC connections on port 8080
15:48:23:981 INFO http_server connectSerial(1, )
15:48:24:003 INFO http_server 192.168.0.x:51752 - - "POST / HTTP/1.1" 200 479
15:48:24:010 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.TypeError'>:connectSerial() takes at least 3 arguments (2 given)</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
15:54:00:505 INFO http_server connectSerial(1, /dev/ttyS1, )
15:54:00:534 INFO http_server 192.168.0.x:51772 - - "POST /RPC2 HTTP/1.0" 200 225
15:54:00:541 INFO http_server <?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>
15:54:00:573 INFO http_server waitOnEvent(4c6f8c, 1, )
NSString *const ourAddr = @"1";
NSString *const dev = @"/dev/ttyS1";
-(IBAction)getResponse:(id) sender{
NSArray *args = [NSArray arrayWithObjects: ourAddr, dev, nil];
NSString *server = @"http://192.168.0.x:8080";
XMLRPCRequest *waitOnEvent = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
[waitOnEvent setMethod:@"connectSerial" withObjects: args];
20:17:20:954 INFO http_server connectSerial(1, /dev/ttyS1, )
20:17:20:975 INFO http_server 192.168.0.x:52253 - - "POST / HTTP/1.1" 200 467
20:17:20:981 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.ValueError'>:Serial interface type must be an integer</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
20:17:20:954信息http_服务器连接串行(1,/dev/ttyS1,)
20:17:20:975信息http_服务器192.168.0.x:52253--“POST/http/1.1”200467
20:17:20:981信息http_服务器
故障码
1.
断层串
类型“exceptions.ValueError”:串行接口类型必须为整数
现在我只需要弄清楚如何将“1”声明为整数,然后同时传递and integer和字符串。到目前为止,我尝试的每件事都给了我一个糟糕的解释…你不能在iOS设备上调用脚本。您可以在OS X中使用,但不能在(AppStore批准的)iOS应用中使用。我猜您需要创建一个包含参数的数组:
NSArray *args = [NSArray arrayWithObjects:ourAddr, dev, nil];
然后将其传递给XMLRPCRequest:
[waitOnEvent setMethod:@"connectSerial" withObject:args];
哇!我发誓我已经试过了。像洛克这样的人更接近于答案。所以它几乎传递了正确的论点。。。刚刚添加了[]和“”,谢谢你的帮助,我很感激!这是我从Python服务器得到的信息:17:19:27:579 INFO http_Server connectSerial(['1','/dev/ttylS1'])17:19:27:600 INFO http_Server 192.168.0.x:52626--“POST/http/1.1”200 479 17:19:27:607 INFO http_Server faultCode 1 faultString type“exceptions.TypeError”:connectSerial()至少有3个参数(2给出)@swbooking如果这个答案正是你想要的,点击答案左侧出现的大“V”将其标记为正确答案:)这样你就不能从你的应用程序调用另一台服务器上运行的脚本了?真的,我只需要这个作为测试。最终,网桥应该连接到web服务器,应用程序将只显示数据。谢谢你的帮助!我的错。我浏览了你的密码。您无法在设备上调用它。您想查找使用NSURL/NSURLRequest/NSURLConnection调用POST方法的示例。太好了!我会查的。再次感谢!