เว็บเซอร์วิสถือเป็นเทคโนโลยีเว็บที่ได้รับความนิยมอีกเทคโนโลยีหนึ่ง หากต้องการศึกษาข้อมูลเกี่ยวกับเว็บเซอร์วิสเพิ่มเติมสามารถอ่านได้ที่วิกิพีเดียเรื่อง เว็บเซอร์วิส สำหรับบทความนี้เน้นเรื่องการสร้างเว็บเซอร์วิสไคลเอนท์เพื่อติดต่อกับเว็บเซอร์วิสเซิร์ฟเวอร์ โดยในที่นี้ใช้กรณีศึกษาการตรวจสอบเลขบัตรประจำตัวประชาชนกับเว็บเซอร์วิสที่ให้บริการโดยกรมสรรพากร 

import sys
import suds

if __name__ == "__main__":
if len(sys.argv) < 2 :
sys.exit()  

client = suds.client.Client('https://rdws.rd.go.th/ServiceRD/CheckTINPINService.asmx?WSDL') 

result = client.service.ServicePIN('anonymous','anonymous', sys.argv[1])

print "ID number:", sys.argv[1]

try:
is_valid = result.diffgram.NewDataSet.CheckingResult.DigitOk
print "The ID is valid:", is_valid
except:
print "The ID is not valid because", result.diffgram.NewDataSet.Message.Description

ทดสอบการทำงานของโปรแกรม

$ python check_id.py 18xxx0000x80x
ID number: 18xxx0000x80x
The ID is valid: true
$ python check_id.py 1800000000000
ID number: 1800000000000
The ID is not valid because เลข PIN ไม่ถูกต้อง เนื่องจาก หมายเลขPINเป็นตัวอักษรหรือเลขPINเป็นตัวอักษรหรือจำนวนหลักเท่ากับ13หลักหรือรูปแบบ PINไม่ถูกต้อง <br> PIN incorrect <br> => "1800000000000"

วิธีการใช้ไลบรารี suds ในขั้นแรก สร้างส่วนที่ใช้เชื่อมต่อกับเว็บเซอร์วิสเซิร์ฟเวอร์โดยใช้ WSDL

client = suds.client.Client(WSDL) 

เมื่อผ่านขั้นตอนนี้ไปแล้วก็สามารถเรียกใช้เวอร์วิสที่ให้บริการได้เลย โดยในที่นี้เป้นตัวอย่างการเรียกใช้งานเซอร์วิสชื่อ SericePIN

result = client.service.ServicePIN('anonymous','anonymous', ID)

ข้อมูลทุกอย่างที่เซอร์วิสตอบกลับมาจะจัดเก็บไว้ในตัวแปร result  ตัวอย่างที่ได้จากการเรียกเซอร์วิสนี่แสดงไว้ดังด้านล่าง

(ServicePINResult){
   schema = 
      (schema){
         _id = "NewDataSet"
         element = 
            (topLevelElement){
               _IsDataSet = "true"
               _name = "NewDataSet"
               complexType = 
                  (localComplexType){
                     choice = 
                        (explicitGroup){
                           _maxOccurs = "unbounded"
                           element = 
                              (localElement){
                                 _name = "Message"
                                 complexType = 
                                    (localComplexType){
                                       sequence = 
                                          (explicitGroup){
                                             element[] = 
                                                (localElement){
                                                   _type = "xs:string"
                                                   _name = "Code"
                                                   _minOccurs = 0
                                                },
                                                (localElement){
                                                   _type = "xs:string"
                                                   _name = "Description"
                                                   _minOccurs = 0
                                                },
                                          }
                                    }
                              }
                        }
                  }
            }
      }
   diffgram = 
      (diffgram){
         NewDataSet = 
            (NewDataSet){
               Message = 
                  (Message){
                     _id = "Message1"
                     _rowOrder = "0"
                     _hasChanges = "inserted"
                     Code = "E00008"
                     Description = "เลข PIN ไม่ถูกต้อง เนื่องจาก หมายเลขPINเป็นตัวอักษรหรือเลขPINเป็นตัวอักษรหรือจำนวนหลักเท่ากับ13หลักหรือรูปแบบ PINไม่ถูกต้อง 
PIN incorrect
=> "181990000680"" } } } }

การเรียกใช้งานเว็บเซอร์วิสโดยใช้ไลบรารี suds นั้นง่ายมาก ทดลองใช้งานกันดูนะครับ


Add comments
name:
E-mail:
Home page:
ค้นหา
Custom Search
Adverties
2008 Ofebia. Creative Commons Attribution 3.0 License