Discussion:
[Tutor] apache python cgi sockets error 13 (resend after joining the tutorial list)
Stewart Lawton
2015-05-07 10:56:53 UTC
Permalink
Hi I have tried Python TCPIP sockets and Unix sockets processed in a python cgi script, called from apache under fedora19. In both cases a permissions error is returned at sock.connect(). I have tried changing permissions x and r w on ALL of user, group, other to no avail. In both cases the cgi script runs if directly executed from /var/www/cgi-bin with no apache involvement.

In both cases all the other (non sockets())  python cgi scripting works in the apache server environment.

I note I needed to enable http in the ferora19 firewall to have the apache server work from locahost or from another device connected to the router. Please find the myUnix2.cgi socket script below. Help appreciated! Regards,              Stewart Lawton
#!/usr/bin/envpython
import cgi
import socket
import sys
defhtmlTop():
 print("""Content-type:text/html\n\n
 <DOCTYPEhtml>
 <html lang="en">
  <head>
       <metacharset="utf-8" />
      <title> MyServer Template </title>
      </head>
      <body>""")
 
defhtmlTail():
 print("""<body/>
      </html> """   )

defcreSockettoServer():
# Create a TCP/IP socket
   sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
   server_address ='../../.././home/johnlawton/workspace/myUnixSock/uds_socket'
   try:
     sock.connect(server_address)
   except socket.error,msg:
     print>>sys.stderr, msg
     sys.exit(1)
#Send data
    message = 'This is the message. It will be repeated.'
#   print >>sys.stderr,'sending "%s"' % message
   sock.sendall(message)
    return

#mainprogram
if __name__ == "__main__":
   try:
        htmlTop()
       creSockettoServer()
       print("Hello World from my Unix2 cgi Script ")
       
        htmlTail()
   except:
       cgi.print_exception()
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options
Alan Gauld
2015-05-07 12:53:56 UTC
Permalink
Can you post again in plain text? The formatting below
is all messed up, indentation and spacing errors abound.
Post by Stewart Lawton
Hi I have tried Python TCPIP sockets and Unix sockets
processed in a python cgi script,
Have you tried opening the same sockets in a script run
in your user space? Its most likely a problem of cgi user
and your user permissions clashing.
Post by Stewart Lawton
#!/usr/bin/envpython
import cgi
import socket
import sys
Note we've lost a space.
Post by Stewart Lawton
print("""Content-type:text/html\n\n
<DOCTYPEhtml>
<html lang="en">
<head>
<metacharset="utf-8" />
<title> MyServer Template </title>
</head>
<body>""")
print("""<body/>
</html> """ )
# Create a TCP/IP socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server_address ='../../.././home/johnlawton/workspace/myUnixSock/uds_socket'
sock.connect(server_address)
print>>sys.stderr, msg
sys.exit(1)
#Send data
message = 'This is the message. It will be repeated.'
# print >>sys.stderr,'sending "%s"' % message
sock.sendall(message)
return
And the indentation above is messed up.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Felix Dietrich
2015-05-07 16:46:33 UTC
Permalink
Post by Stewart Lawton
Hi I have tried Python TCPIP sockets and Unix sockets processed in a
python cgi script, called from apache under fedora19. In both cases a
permissions error is returned at sock.connect(). I have tried changing
permissions x and r w on ALL of user, group, other to no avail. In
both cases the cgi script runs if directly executed from
/var/www/cgi-bin with no apache involvement.
Make sure that you not only set the permissions for the socket file but
also for the directories leading to the file. The man-pages for AF_UNIX
(man unix) state under the section NOTES:

In the Linux implementation, sockets which are visible in the
filesystem honor the permissions of the directory they are in.
Their owner, group and their permissions can be changed. Creation
of a new socket will fail if the process does not have write and
search (execute) permission on the directory the socket is created
in. Connecting to the socket object requires read/write permission.
Post by Stewart Lawton
server_address = '../../.././home/johnlawton/workspace/myUnixSock/uds_socket'
You are using a relative path here. Are you certain about the working
directory of the interpreter?

--
Felix Dietrich
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...