Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44093 > unrolled thread

How to connect to a website?

Started bywebmaster@terradon.nl
First post2013-04-22 09:16 -0700
Last post2013-04-22 17:40 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  How to connect to a website? webmaster@terradon.nl - 2013-04-22 09:16 -0700
    Re: How to connect to a website? John Gordon <gordon@panix.com> - 2013-04-22 16:38 +0000
      Re: How to connect to a website? webmaster@terradon.nl - 2013-04-22 12:56 -0700
    Re: How to connect to a website? MRAB <python@mrabarnett.plus.com> - 2013-04-22 17:40 +0100

#44093 — How to connect to a website?

Fromwebmaster@terradon.nl
Date2013-04-22 09:16 -0700
SubjectHow to connect to a website?
Message-ID<566767a8-35cc-47f2-9f75-032ce5629b44@googlegroups.com>
Hi,
i just try to connect to a website, read that page and display the rules get from it.
Then i get this error message:

  File "D:/Python/Py projects/socket test/sockettest.py", line 21, in <module>
    fileobj.write("GET "+filename+" HTTP/1.0\n\n")
io.UnsupportedOperation: not writable

My code:

# import sys for handling command line argument 
# import socket for network communications 
import sys, socket 
 
# hard-wire the port number for safety's sake 
# then take the names of the host and file from the command line 
port = 80 
host = 'www.xxxxxxxx.nl' 
filename = 'index.php' 

# create a socket object called 'c' 
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

# connect to the socket 
c.connect((host, port)) 

# create a file-like object to read 
fileobj = c.makefile('r', 1024) 

# Ask the server for the file 
fileobj.write("GET "+filename+" HTTP/1.0\n\n") 

# read the lines of the file object into a buffer, buff 
buff = fileobj.readlines() 

# step through the buffer, printing each line 
for line in buff: 
    print (line)


I started with invent games with python (book 1 & 2)
Now I want to write a multiplayergame, which connects to a website, where all players and gamedata will be stored/controlled.
Players need to subscribe and to login via the game software. (executable, made from python script)
Sending gamedata preferable in JSON, because of low traffic resources then.
No idea about how authentication proces should be done....

I made many searches with Google, but got confused about my first steps. 
I am new to python, but code for many years in php/mysql. Spent most time in an online chessgame project.

[toc] | [next] | [standalone]


#44097

FromJohn Gordon <gordon@panix.com>
Date2013-04-22 16:38 +0000
Message-ID<kl3p2n$klr$1@reader1.panix.com>
In reply to#44093
In <566767a8-35cc-47f2-9f75-032ce5629b44@googlegroups.com> webmaster@terradon.nl writes:

> Hi,
> i just try to connect to a website, read that page and display the rules get from it.
> Then i get this error message:

>   File "D:/Python/Py projects/socket test/sockettest.py", line 21, in <module>
>     fileobj.write("GET "+filename+" HTTP/1.0\n\n")
> io.UnsupportedOperation: not writable

I haven't worked with the socket library, but I think this error is because
you specified a mode of 'r' when calling makefile().  fileobj is read-only,
and you're trying to write to it.

If you just want to connect to a website, try using the urllib2 module
instead of socket.  It's higher-level and handles a lot of details for
you.  Here's an example:

    import urllib2

    request = urllib2.Request('http://www.voidspace.org.uk')
    response = urllib2.urlopen(request)
    content = response.readlines()

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#44106

Fromwebmaster@terradon.nl
Date2013-04-22 12:56 -0700
Message-ID<49b55b70-0283-484c-b517-5b1c5b1a2678@googlegroups.com>
In reply to#44097
thanks!
solved with:

import urllib.request
import urllib.parse

user = 'user'
pw = 'password'

login_url = 'http://www.riskopoly.nl/test/index.php'

data = urllib.parse.urlencode({'user': user, 'pw': pw})
data = data.encode('utf-8')
# adding charset parameter to the Content-Type header.
request = urllib.request.Request(login_url)
request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")

f = urllib.request.urlopen(request, data)
print(f.read().decode('utf-8'))

And then i get next answer:
<pre>Array
(
    [pw] => password
    [user] => user
)
</pre>

Solved and thanks again:)

[toc] | [prev] | [next] | [standalone]


#44098

FromMRAB <python@mrabarnett.plus.com>
Date2013-04-22 17:40 +0100
Message-ID<mailman.932.1366648821.3114.python-list@python.org>
In reply to#44093
On 22/04/2013 17:16, webmaster@terradon.nl wrote:
> Hi,
> i just try to connect to a website, read that page and display the rules get from it.
> Then i get this error message:
>
>    File "D:/Python/Py projects/socket test/sockettest.py", line 21, in <module>
>      fileobj.write("GET "+filename+" HTTP/1.0\n\n")
> io.UnsupportedOperation: not writable
>
> My code:
>
> # import sys for handling command line argument
> # import socket for network communications
> import sys, socket
>
> # hard-wire the port number for safety's sake
> # then take the names of the host and file from the command line
> port = 80
> host = 'www.xxxxxxxx.nl'
> filename = 'index.php'
>
> # create a socket object called 'c'
> c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> # connect to the socket
> c.connect((host, port))
>
> # create a file-like object to read
> fileobj = c.makefile('r', 1024)

You're creating a file-like object for reading...
>
> # Ask the server for the file
> fileobj.write("GET "+filename+" HTTP/1.0\n\n")
>
...and then trying to write to it.

> # read the lines of the file object into a buffer, buff
> buff = fileobj.readlines()
>
> # step through the buffer, printing each line
> for line in buff:
>      print (line)
>
[snip]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web