Skip to content

Commit 648be8c

Browse files
author
Montana Flynn
committed
Python 3 complete
1 parent 6983f0a commit 648be8c

17 files changed

Lines changed: 212 additions & 0 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ currently the following output [targets](/src/targets) are supported:
3434
- [CoHTTP](https://github.com/mirage/ocaml-cohttp)
3535
- PHP
3636
- [ext-curl](http://php.net/manual/en/book.curl.php)
37+
- Python
38+
- [Python 3](https://docs.python.org/3/library/http.client.html)
3739
- [Wget](https://www.gnu.org/software/wget/)
3840

3941
## Installation

src/targets/python/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('require-directory')(module)

src/targets/python/info.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = {
4+
key: 'python',
5+
title: 'Python',
6+
extname: '.py',
7+
default: 'python3'
8+
}

src/targets/python/python3.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict'
2+
3+
var util = require('util')
4+
5+
module.exports = function (source, options) {
6+
// Start Request
7+
var code = []
8+
code.push('import http.client\n')
9+
10+
// Check which protocol to be used for the client connection
11+
var protocol = source.uriObj.protocol
12+
if (protocol === 'https:') {
13+
code.push(util.format('conn = http.client.HTTPSConnection("%s")', source.uriObj.host))
14+
} else {
15+
code.push(util.format('conn = http.client.HTTPConnection("%s")', source.uriObj.host))
16+
}
17+
18+
// Create payload string if it exists
19+
var payload = JSON.stringify(source.postData.text)
20+
if (payload) {
21+
code.push(util.format('payload = %s', payload))
22+
}
23+
24+
// Create Headers
25+
var header
26+
var headers = source.allHeaders
27+
var headerCount = Object.keys(headers).length
28+
if (headerCount === 1) {
29+
for (header in headers) {
30+
code.push(util.format('headers = { \'%s\': "%s" }', header, headers[header]))
31+
}
32+
} else if (headerCount > 1) {
33+
var headerLine
34+
var count = 1
35+
code.push('headers = {')
36+
for (header in headers) {
37+
if (count++ !== headerCount) {
38+
headerLine = util.format(' \'%s\': "%s",', header, headers[header])
39+
} else {
40+
headerLine = util.format(' \'%s\': "%s"', header, headers[header])
41+
}
42+
code.push(headerLine)
43+
}
44+
code.push(' }')
45+
}
46+
47+
// Make Request
48+
var method = source.method
49+
var path = source.uriObj.path
50+
if (payload && headerCount) {
51+
code.push(util.format('conn.request("%s", "%s", payload, headers)', method, path))
52+
} else if (payload && !headerCount) {
53+
code.push(util.format('conn.request("%s", "%s", payload)', method, path))
54+
} else if (!payload && headerCount) {
55+
code.push(util.format('conn.request("%s", "%s", headers = headers)', method, path))
56+
} else {
57+
code.push(util.format('conn.request("%s", "%s")', method, path))
58+
}
59+
60+
// Get Response
61+
code.push('res = conn.getresponse()')
62+
code.push('data = res.read()')
63+
code.push('print(res.status)')
64+
code.push('print(data)')
65+
66+
// console.log(code)
67+
return code.join('\n')
68+
}
69+
70+
module.exports.info = {
71+
key: 'python3',
72+
title: 'http.client',
73+
link: 'https://docs.python.org/3/library/http.client.html',
74+
description: 'Python3 HTTP Client'
75+
}

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@
7474
}
7575
]
7676
},
77+
{
78+
"key": "python",
79+
"title": "Python",
80+
"extname": ".py",
81+
"default": "python3",
82+
"clients": [
83+
{
84+
"key": "python3",
85+
"title": "http.client",
86+
"link": "https://docs.python.org/3/library/http.client.html",
87+
"description": "Python3 HTTP Client"
88+
}
89+
]
90+
},
7791
{
7892
"key": "objc",
7993
"title": "Objective-C",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import http.client
2+
3+
conn = http.client.HTTPConnection("mockbin.com")
4+
payload = "foo=bar&hello=world"
5+
headers = { 'content-type': "application/x-www-form-urlencoded" }
6+
conn.request("POST", "/har?", payload, headers)
7+
res = conn.getresponse()
8+
data = res.read()
9+
print(res.status)
10+
print(data)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import http.client
2+
3+
conn = http.client.HTTPConnection("mockbin.com")
4+
payload = "{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }"
5+
headers = { 'content-type': "application/json" }
6+
conn.request("POST", "/har?", payload, headers)
7+
res = conn.getresponse()
8+
data = res.read()
9+
print(res.status)
10+
print(data)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import http.client
2+
3+
conn = http.client.HTTPConnection("mockbin.com")
4+
headers = { 'cookie': "foo=bar; bar=baz" }
5+
conn.request("POST", "/har?", headers = headers)
6+
res = conn.getresponse()
7+
data = res.read()
8+
print(res.status)
9+
print(data)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import http.client
2+
3+
conn = http.client.HTTPConnection("mockbin.com")
4+
payload = "foo=bar"
5+
headers = {
6+
'cookie': "foo=bar; bar=baz",
7+
'accept': "application/json",
8+
'content-type': "application/x-www-form-urlencoded"
9+
}
10+
conn.request("POST", "/har?foo=bar&foo=baz&baz=abc&key=value", payload, headers)
11+
res = conn.getresponse()
12+
data = res.read()
13+
print(res.status)
14+
print(data)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import http.client
2+
3+
conn = http.client.HTTPConnection("mockbin.com")
4+
headers = {
5+
'accept': "application/json",
6+
'x-foo': "Bar"
7+
}
8+
conn.request("GET", "/har?", headers = headers)
9+
res = conn.getresponse()
10+
data = res.read()
11+
print(res.status)
12+
print(data)

0 commit comments

Comments
 (0)