2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでHTTP POST

Posted at

Python 3時間目~4時間目
HTTP POSTを行いたい。

環境 Python 3.6

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request

#自己証明サイトへのポスト時に
#import ssl
#ssl._create_default_https_context = ssl._create_unverified_context

class SampleHttp:

    def get(self):
        url = 'https://www.yahoo.co.jp/'
        with urllib.request.urlopen(url) as response:
            html = response.read()
            print(html)

    def post(selfs):
        #405 エラー
        #url = 'https://www.google.co.jp'
        #values = {'q': 'hello'}

        url = 'https://www.yahoo.co.jp/'
        values = {'q': 'hello'}

        data = urllib.parse.urlencode(values)
        data = data.encode('ascii')
        req = urllib.request.Request(url, data)
        with urllib.request.urlopen(req) as response:
            html = str(response.read(), "utf-8")
            print(html)
            response.close()

#
http = SampleHttp()
http.post()
  • unicodeという関数はpython 2の時代の物。python 2とpython 3で文字列の仕様が結構変わった?
  • googleへのポストに失敗・・・
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?