Skip to content

Instantly share code, notes, and snippets.

@clayg
Created August 26, 2020 16:59
Show Gist options
  • Select an option

  • Save clayg/eea76851a440a851196627348012295f to your computer and use it in GitHub Desktop.

Select an option

Save clayg/eea76851a440a851196627348012295f to your computer and use it in GitHub Desktop.
hold a connection open
#!/usr/bin/env python
import sys
import argparse
from swift.common.storage_policy import POLICIES
from swift.common.bufferedhttp import http_connect
from swift.common.utils import Timestamp
SWIFT_DIR = '/etc/swift'
parser = argparse.ArgumentParser()
parser.add_argument('--policy', '-P', default=0, type=int,
help='the policy number')
parser.add_argument('--device', '-d', required=True,
help='the device name')
parser.add_argument('--part', '-p', required=True, type=int,
help='the part number')
# parser.add_argument('--wait', '-w', type=float, default=3.0,
# help='seconds to hold connection')
def main():
args = parser.parse_args()
policy = POLICIES[args.policy]
policy.load_ring(SWIFT_DIR)
body = 'asdf'.encode('utf8')
for dev in policy.object_ring.devs:
if not dev:
continue
if dev['device'] == args.device:
break
else:
return 'ERROR: unable to find device %s in %s/%s.ring.gz' % (
args.device, SWIFT_DIR, policy.ring_name)
headers = {
'X-Backend-Storage-Policy-Index': args.policy,
'Expect': '100-continue',
'Content-Length': len(body),
'X-Timestamp': Timestamp.now().internal,
'Content-Type': 'application/junk',
}
conn = http_connect(dev['ip'], dev['port'], dev['device'], args.part,
'PUT', '/a/c/o', headers=headers)
resp = conn.getexpect()
if resp.status != 100:
print(resp.read())
return 'ERROR: got expect response %s' % resp.status
# N.B. we've started request, but haven't sent the body yet
raw_input('Press ENTER to continue')
conn.send(body)
resp = conn.getresponse()
print(resp.status)
print(resp.getheaders())
print(resp.read())
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment