Categories
python

Using Python to Automate SSH Connection and Run Commands

We will be using a  http://fitztrev.github.io/shuttle/ and the pexpect python library. Here is what the shuttle menu looks like. Mine could be named better. #!/usr/bin/python from pexpect import * import pexpect PROMPT = [‘# ‘, ‘>>> ‘, ‘> ‘,’\$ ‘] def send_command(child, cmd): child.sendline(cmd) child.expect(PROMPT) print child.before child.interact() def connect(user, host, password): ssh_newkey = ‘Are you […]

Categories
ios swift

Making an HTTP POST Request in Swift

In this example I will be uploading an image to a node.js server. func makePostRequest(){ // create a instantiate a session let session = NSURLSession.sharedSession() // set url to POST to var url = NSURL(string: “http://yourdomain.com/post”) // create the request object var request = NSMutableURLRequest(URL: url!) request.HTTPMethod = “POST” request.HTTPBody = YOUR_REQUEST_BODY // set up the […]

Categories
ios swift

Creating a POST request body in Swift

// createRequestBody() func createRequestBody(photo:NSData){ var boundary:String = “——WebKitFormBoundaryasdas543wfsdfs5453533d3sdfsf3” var contentType = “multipart/form-data; boundary=\(boundary)” request.HTTPMethod = “POST” request.addValue(contentType, forHTTPHeaderField: “Content-Type”) var body = NSMutableData() body.appendData(NSString(format: “\r\n–%@\r\n”, boundary).dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(NSString(format: “Content-Disposition: form-data; name=\”photo\”; filename=\”photo.jpeg\”\r\n”).dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(NSString(format:”Content-Type: image/jpeg\r\n\r\n”).dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(photo) body.appendData(NSString(format: “\r\n–%@\r\n”, boundary).dataUsingEncoding(NSUTF8StringEncoding)!) println(body) // This will be the request body to post to your api/service }  Example of request body […]