Encode query with Python

Encode query with Python - WABI

    Encode query with Python
    From WABI
    Jump to: navigation, search

    Contents

    [hide]

        1 Summary
        2 Description
        3 Sample program
        4 Link


    Summary

    Result may not be able to be retrieved normally, if query including non alphanumeric character is used with Python.

    Description

    You have to encode your query. If qPath is a query to be encoded, please do as follows.

    import urllib
    urllib.urlencode({'<parameter_name>':qPath,})

    Sample program

    This tutorial introduces a example that retrieve entries which feature-key is 'rRNA', qualifier name is 'product' and has '16s ribosomal RNA' in qualifier value with searchByXMLPath of ARSA.

    Download this program

    import socket
    import urllib

    #set server
    host = "xml.nig.ac.jp"
    port =  80

    #set API server
    url = "/rest/Invoke"

    qPath = "/ENTRY/DDBJ/feature-table/feature/f_key=='rRNA' AND "
    qPath = qPath + "(/ENTRY/DDBJ/feature-table/feature{/f_key=='rRNA' AND "
    qPath = qPath + "/f_quals/qualifier{/q_name=='product' AND /q_value='16S ribosomal RNA'}}) "
    rPath = "/ENTRY/DDBJ/primary-accession,/ENTRY/DDBJ/definition"

    offset = "1";
    count = "10";

    query = urllib.urlencode({'queryPath':qPath,'returnPath':rPath,'offset':offset,'count':count});

    #set parameter
    query = "service=ARSA&method=searchByXMLPath&"+ query;
    #make connection
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        sock.connect((host, port))
        sock.send("POST " + url + " HTTP/1.0\n")
        sock.send("Content-Type: application/x-www-form-urlencoded\n")
        sock.send("User-Agent: python/socket\n")
        sock.send("Content-Length:" + `len(query)` + "\n\n")
        sock.send(query)
    except socket.error, e:
        print 'Error: %s' % e

    while 1:
        rcvmsg = sock.recv(1024)
        print rcvmsg,
        if rcvmsg == :
            break
    sock.close

posted on 2012-03-19 00:12  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/03/19/2405379.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值